PHP 7  has been around for well over a year now, and yet only 4.5% of sites have made the upgrade from PHP 5. With users and their hosts lagging behind, a large number of sites remain more vulnerable to hacking and performance issues than their updated counterparts.

As a WordPress developer, you hold much more sway and influence than you might think. Brands such as Yoast are asking you to use that power to help upgrade the internet as a whole. You can do this by adding nudges to your plugins using Yoast’s WHIP framework. This helps you inform the user about why they should always run the latest PHP version. It also makes your plugin run more smoothly in production, so it’s a win-win proposition.

In this article, we’ll explain why it’s worth getting as many people as possible to upgrade to PHP 7. Then, we’ll show you how to add a PHP version nudge to your own WordPress plugin in three steps. Let’s get started!

Why It’s Worth Compelling Users to Upgrade to PHP 7

WordPress.org always advocates that users build their sites using the latest version of PHP. This is for many reasons, including security, implementing modern coding standards, and generally providing an improved performance.

PHP 7 landing page
PHP 7 has a number of benefits over its previous versions – most notably, speed.

Unfortunately, of websites using PHP, a whopping 94.5% are still running the previous version – and a quarter are running on the most recent PHP 5.6. Developers will often let new major versions mature before switching. PHP 7 has now been available for a year and has proven itself in production, yet only 4.6% of PHP users have upgraded.

More recently, Yoast – developers of the Yoast SEO plugin – has begun pushing users to upgrade from within the plugin over the last few releases. Though initially fearful such a nudge might annoy their users, de Valk says that “Negative feedback has been absolutely minimal.” Yoast’s consensus is that developers should be compelling users to upgrade. On the whole, when newer and better versions of software exist, there is very little reason not to update them.

Because of this, Yoast has released a public package called WHIP, which helps you implement your own PHP upgrade nudge within a WordPress plugin. It’s found on GitHub, and is easy to implement. With that in mind, let’s take a look at how to do so.

How to Implement the WHIP Codebase Into Your Own Plugin (in 3 Steps)

Of course, this will be most useful to those who build and distribute WordPress plugins. Over the next three steps, we’ll show you how easy it is to add PHP upgrade notices to your plugins with Yoast’s WHIP.

1. Install Local Development Prerequisites for the WHIP Codebase

To install the WHIP codebase in your plugin, you’ll first need to install Composer – a dependency manager for PHP, which enables you to install WHIP rapidly:

Composer dependency manager

To get started, download Composer to your computer. You can enter the following commands in to your console to do this.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Next, follow the appropriate installation steps for your operating system – Windows or Linux, Unix, and OS X/macOS. At this point, you should already have a plugin to distribute that you plan on adding a nudge to. If so, make sure you’ve set up a local development environment (loaded with WordPress and your plugin) for you to work in.

2. Install the WHIP Core Codebase to Your Plugin

Once Composer is installed, and your local development environment is ready, navigate to your target plugin folder within the console. Your plugin is likely under a path similar to /wp-content/plugins/example, where example is the name of your plugin.

Installing Yoast WHIP using composer
An example of installing Yoast WHIP using Composer.

After locating your plugin folder, simply type the installation command for WHIP into the console:

composer require yoast/whip

This will add a new folder called vendor to your plugin. However, that’s not all you need to do. New Composer users still need to notify your plugin that these new files exist, and you’ll need to ‘require’ the Composer autoloader.php file somewhere in your plugin’s code. Here’s an example of code that works within the main plugin file:

if ( file_exists( plugin_dir_path(__FILE__) . '/vendor/autoload.php' ) ) {
    require plugin_dir_path(__FILE__) . '/vendor/autoload.php';
}

At this point, the WHIP codebase should be included with the rest of your plugin. The final step is to actually trigger the front end nudge.

3. Trigger the Front End Notice for Old PHP Versions

For the final step, you’ll need to trigger the notification for users running an old PHP version. Once you’ve included the Composer autoload.php file, you’ll want to add the function that checks for the PHP version, then return a notification if it is below a certain number.

You can choose your minimum required level of PHP depending on what you think is appropriate. In this example, you can see it triggers only if the PHP version is lower than 5.6:

whip_wp_check_versions( array(
    'php' => '>=5.6',
) );

The WHIP function for this is highly configurable. For example, there are options for customizing:

  • Which version of PHP you want to check for, depending on how strict you want your plugin to be.
  • Host-specific messages, if you are a web host distributing WordPress plugins.
  • Recommendation links, if you don’t want to use Yoast’s provided landing pages.

Once you’ve saved your changes, check your WordPress dashboard in your browser for the new message.

Yoast WHIP Notification in action
An example of the Yoast WHIP PHP upgrade notification.

Upgrading your PHP version has wide-reaching ramifications for users, so we believe a dashboard message is well worth implementing – and Yoast’s WHIP codebase makes it a snap!

Conclusion

Compelling users to upgrade to PHP 7 helps improve the performance of your plugins, thanks to the many benefits the latest version has over its predecessors. While the version notice may seem unneeded at first, your users will probably be thankful once they’ve made the switch.

To install Yoast’s WHIP codebase in your plugins to help encourage users to switch, you need to:

  1. Install local development prerequisites for the WHIP codebase.
  2. Install the WHIP core codebase to your plugin.
  3. Trigger the front end notice for old PHP versions.

Do you have any questions about adding a PHP version upgrade notice to your WordPress plugin? Let us know in the comments section below!

Image credits: Andrew Measham.

John Hughes

John is a blogging addict, WordPress fanatic, and a staff writer for WordCandy.

The post How to Remind Users to Upgrade to PHP 7 Using the WHIP Codebase appeared first on Torque.

Share This