How WordPress Plugins can avoid being marked as broken by Users

A problem you’ll face when writing a WordPress Plugin is how to maintain compatibility with older versions of WordPress. If you accidentally use a function whose behaviour has changed recently, you could end up with a broken plugin.

Another aspect you also need to consider is compatibility with older versions of PHP and MySQL. While it may make sense to use modern PHP features like namespaces, doing so could mean over 50% users of WordPress who are still on PHP 5.2 will not be able to use your plugin.

Worse yet, if users come across such an incompatibility it can easily be marked as broken, thus affecting your plugin’s rating.

Sometimes it won’t be immediately apparent that you have a broken plugin on your hands. The incompatibility you introduce will be very subtle and will be easily missed if you aren’t explicitly testing for that scenario.

For instance, if your plugin does manipulation of JPEG images, you might use the PHP gd_info function to test for this feature. However there is a subtle gotcha here. In PHP 5.3 the array key returned by this function was changed to JPEG Support, while earlier versions used JPG Support instead.

Currently does not allow developers to specify what version of PHP or MySQL is required by their plugins. The only variables it lists are,

  • Requires: 3.5
  • Compatible up to: 3.8.1
  • Last Updated: 2013-11-8
  • Downloads: 15,000

It would be nicer if it were to also list PHP and MySQL compatibility. The plugin File Header could easily be adapted to include this meta-information.

  • PHP: 5.3+
  • MySQL: 5.5+

Doing this would help inform users before they go about installing plugins on an older configuration. Thus help prevent plugins being marked as broken when they are merely incompatible with that user’s system.

However since this isn’t currently possible you need to look at alternatives. WordPress uses a wp_check_php_mysql_versions function to do it’s own PHP and MySQL checks but does not exposes this to developers.

If you absolutely can’t support an older system configuration, you can add an explicit if statement in your plugin to check whether your minimum system requirement is met. Then prompt the user that the plugin is incompatible with your configuration and ask the user to upgrade.

For instance if you are using namespaces in your plugin you need at least PHP 5.3. Don’t be tempted to use PHP’s core predefined constants, PHP_MAJOR_VERSION or PHP_MINOR_VERSION. They were introduced only after PHP 5.2.7. Instead use the version_compare function which was designed to do just this, compare version strings.

if (version_compare(phpversion(), '5.3.0', '>=')) {
  // load your plugin here
} else {
  // show a warning to the user that their system is too old

Now even if the user did install your plugin on an incompatible configuration they would be informed that their system needs to be upgraded instead of just seeing broken plugin error messages.