WordPress Broke My Site!

I hear the battle cry of website owners every time WordPress releases an update: “The update broke my site/theme/plugins/heart!” Usually I ignore the wimperings with the knowledge that “good” developers test their stuff with the pre-release versions so there aren’t any surprises.

Well the surprise is on me, now 🙁 The WordPress 3.6 update broke my sites.

The Problem:

Here’s the story. I build websites based on WordPress for a small company called Golf Tailor, where we rely heavily on custom themes and plugins that are a mixture of premium and in-house developed code. In my effort to make those sites load a little cleaner, I started using a function I read about online called has_shortcode that I could combine with wp_enqueue_x to only load scripts and styles when needed on a page.

This worked beautifully until 3.6 was released and one of my sites automatically updated. I usually update them myself, but this one is hosted with WPEngine, which will upgrade your site to the latest release unless you specifically ask them not to. Pretty nice feature, until one of my pages quit displaying properly.

After a quick search, I found the culprit was the excellent new function in WP 3.6: has_shortcode. The difference was that the official WordPress version takes two parameters, where the one I was using only took one. That little difference made a huge difference!

The Fix:

Since their implementation of it is essentially the same as the one I was using, this is a simple fix: change all the calls to has_shortcode to pass both $content AND $shortcode.

What Could I Have Done Differently?

First, I thought I was actually doing good when I wrapped my function definition inside a function_exists. That tells the rest of the scripts that if there’s already a function with that name then use it and ignore mine. This is good practice, but in this case the parameters being different made it not work.

Two additional steps I should have taken (and usually do, this one just caught me off my game) are to prefix custom functions, or encapsulate them in a class.