WordPress short tag attributes not parsing?

I recently had an issue with wordpress 3.9.1 short tags not parsing correctly. It would call the appropriate function but not populate any of the appropriate attributes. Infuriatningly the issue was only on my live server and not on my development machine so it was a lot of trial and error to track it down. After investigating the problem in the third party plugin for a while I eventually tracked the problem down to the following regular expressions returning NULL.

$text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);

Checking the php logs had the following error:

Warning: preg_replace() [function.preg-replace]: Compilation failed: unknown option bit(s) set at offset -1 in /path/to/public_html/wp-includes/shortcodes.php on line 257

The problem was an I was running version 7.8 of PCRE (Perl-compatible regular expression) libraries. A quick update to 8.21 solved the problem straight away.

yum update pcre

WordPress and nginx infinite loop when using a reverse proxy

I have recently given my server setup a bit of an upgrade moving from an oldish version of lighttpd to nginx 1.6 compiled with the google pagespeed module. I wrote about the speed benefits in a seperate post.

When I was upgrading I had both servers setup side by side with nginx running on port 82 and setup a quick lighttpd reverse proxy from lighttpd to nginx so that I could keep all my sites up whilst experimenting with nginx config. When forwarding from lighttpd to nginx at a wordpress install it seemed to get itself into an infinite loop redirecting over and over to the homepage.  The problem could be tracked down to the redirect_canonical() function in wp-includes/canonical.php.

The check to see if we were on the canonical version of the url was failing and redirecting to itself.  I believe it was because the port in PHPs’ $_SERVER variable were different so the url did not match and WordPress would continue redirecting to the url forever. A quick fix was to remove the redirect_canonical filter with the following filter:

remove_filter('template_redirect', 'redirect_canonical');

(credits http://www.violato.net/blog/php/88-wordpress-did-infinite-301-redirect-loop)

The more permanent solution for me which may not be appropriate for everyone was to have all traffic go directly to nginx and remove the reverse proxy after I had finished testing other areas of the site. Using the filter above amy end up with you having multiple versions of the same page indexed by google if you have several versions of the same page setup for translations etc.

Search by Product Tag for Woocommerce


Download at wordpress.org

UPDATE 14/03/2014: Version 0.3.1 has been released on wordpress.org get it using the download above. It removes all duplicates and has compatibility with WPML plugin as well as testing up to wordpress 3.8.1 and woocommerce 2.1.5

The default install of woocommerce does not come with any searching capabilities based on the tags that can be assigned to each product.

I have written this plugin to add this capability to the search on both the front end of your woocommerce site and in the wordpress admin site. There is no extra config apart from installation and activation of the plugin.

You can search by multiple tags by splitting your search with a comma. For example to search for all products tagged with toys and helicopter enter into the search box: “toys, helicopter” and all products with those tags will be returned in the search.

The search functionality in woocommerce doesn’t search by product tags by default. This simple plugin adds this functionality to both the admin site and regular search. It is very useful if you want to add some extra meta information to a product that will increase the discoverability on certain products.

If you have any issues or feedback feel free to leave a comment on this post and I will try and sort is as soon as possible

Disclaimer: This plugin does a mysql query with several joins upon each search. If you have an extremely large store (Eg 20, 000 products) you may be better off using a more generic search solution that builds its own optimised indexes for more speedy searching. If however you own a comparatively small store (less than 5, 000 products) this plugin will probably suit you better.

(Image Credit Patrick Feller  Flickr)

Using tables to replace images in HTML Newsletters, a tool using FileReader + Canvas javascript

OK, Yes I know it sounds mental, but consider the latest email that came from apple selling tickets for their WWDC tickets today. When it arrived in my inbox today it looked like this:

When you enable images it looks like this:

It’s nicer but wouldn’t it be nicer if there was a better in-between stage before the ‘display images’ button was pushed. What if there was a way to display the nice image as a html table so it could be sent in an email and look half decent before ‘display images’ is pressed.

I decided to knock up a quick tool that does exactly that. I wanted a completely client side solution so I used the FileReader api. This does mean that browser compatibility is not wide and the tool doesn’t do any degradation, if your browser doesn’t support it the error console is probably the best place to tell it hasn’t worked. You can also use the sample image if you run in to problems using your own. When I feed the apple artwork through the tool and replace the image the email now looks like this:

Also: view it in your browser. Once the image data has been accessed using the FileReader api it loads the data into a canvas element it samples the image at user defined intervals to get the rgb values. Each of the samples it takes is a <td> in the table with a individual colour. Use firebug / browser developer tools to view the tables output.

Youtube hit count spinner

As seen in countless TV adverts and movies. If you drag the button to your bookmark menu and click it whilst on a youtube video it will make your hitcount start growing really fast. If it needs to be said this is just an effect and doesn’t actually increase the amount of views your video has according to youtube its just a effect in your browser.

To to see it in action visit code.mattyl.co.uk/youtube-hit-count-spinner/ drag the button to your bookmarks to use it on youtube.com.

I wrote it mainly to practice some non-jquery javascript and I wanted to do a bookmarklet. So if you have any suggestions to improve the javascript let me know here or you can also fork me on github.

Personal site of Matthew Lawson, Web developer