As long as I’ve been working with WordPress, the conversation (and I do use this term loosely) around what versions of PHP it should or shouldn’t support has been ongoing.
- The short of it is that some see it as an application as large and popular as WordPress that powers so much of the CMS-based websites should be able to use its clout to foster more innovative solutions.
- Others see that people don’t often upgrade, are on budget hosts, or simply do not care (let alone bother to know) what version of PHP they are running.
These two points could yield a post and comments for days, but I’m not interested in that. Because my team and I are the business of building custom solutions for others, we have the flexibility of taking stock of where the users host their site or application, deploying it, and leveraging features of PHP offered by their hosts.
There are, however, times where I’ve encountered some people running versions of PHP as low as 5.3.
So what then?
Handle PHP Version Support in WordPress
First, the WordPress requirements page specifically mentions this:
If you are in a legacy environment where you only have older PHP or MySQL versions, WordPress also works with PHP 5.2.4+ and MySQL 5.0+, but these versions have reached official End Of Life.
So, to be clear, it does run on older versions of PHP but, as developers, we’ll be severely limited by what we can do with the language. And, as the page mentions, the security implications and “end of life” support of the version of the language should not be ignored.
Whenever I work with a plugin or something that’s going to run on WordPress and I know it’s going to have to run on a variety of hosts, I do provide a bare minimum of support. This usually PHP 5.6.20.
If the environment in which the project is going to run doesn’t support that, then I’ll give notice and prevent the project from running. In the context of a plugin, it’s likely best to hook into the register_activation_hook function.
To do that, you can write something like this:
Granted, it’s simple, and the display uses the standard wp_die styling, but it works as well. If you have more experience or time, it’s possible also to run the checks a bit different and provide your own styling.
The purpose of this post isn’t how to do that. Instead, the points are:
- determine what version of PHP you’re going to support,
- compare the version of PHP running on the server,
- render a message if the version of PHP on the server is less than what you’ll support,
- gracefully exit.
How you end up rendering this is up to you, but the gist of the code above should be enough to get you started.
And if you really want to do a deep dive on handling strings in PHP, I recommend Carl’s post on handling strings in PHP.