How to move your WordPress site to PHP7 from an older version of PHP

There are a few steps to move your WordPress site to PHP7. Recently I have been doing these migrations for clients and my own website, from PHP5.x, to PHP7. So I am sharing the steps it took to get there and some tools that help and pitfalls to avoid.

Your first question may be why would want to move your WordPress site to PHP7?

There are a couple good reasons. First, looks like WordPress will be bumping up the WordPress from 5.6 to 7.0 by the middle of 2017. As you might know, PHP is the server-side scripting language which WordPress core, themes, and plugins are written in. WordPress is already coded and able to run on PHP 7. Speed is the second reason. PHP7 was built to compete with Facebook’s HVVM and from benchmarks, it looks like it as least twice as fast as the previous version of PHP. On Yoast’s blog there are even more reasons if you want to dig deeper into it.

Some of you may be asking what happened to PHP 6?

If you have not already made the move, currently you are likely hosting your website some 5.x version of PHP. PHP6 was in development but met some snags during the implementation of language-integrated Unicode. So, the development team scrapped 6 and started on a new version. At this point, the PHP core team was compelled to seriously think about the name of the new major release. And finally, after a vote in July 2014, the development team decided to skip the logical number 6 and launch the next major release calling it PHP7. So, they abandoned PHP6 and never released it.

Steps to prepare for the Move to PHP7

In preparation for moving your site to PHP7 you should do the following:

1) Check to see your host supports PHP7
2) Before beginning to make code changes backup your site
3) Become familiar with the changes in PHP7. There is a full list of changes including the backward incompatible changes.

WordPress Plugins to the Rescue

You will want to installing a couple of plugins to help determine what, if any, code changes will be required to make your site compatible with PHP7. There are two plugins I found very helpful in making the move. They aid in finding PHP7 incompatibilities with your current themes and plugins.

PHP Compatibility Checker Plugin

The first plugin you should install is the PHP7 Compatibility Checker which checks both your theme and your plugins. You can use this plugin on any hosting servers not only WPEngine servers.

This plugin will scan theme and plugin code inside your WordPress file system and give you back a report of compatibility issues for you to fix.

Once installed go to Tools menu and choose PHP Compatibility. From here choose to either scan all themes and all plugins or only active theme and active plugins only.

PHP7 Compatibility Checker options and scanner screenshot

After running the scan a report will print out for each item scanned. Toggle details for each item that has errors and/or warnings to see which PHP files require updates. The line number and the problem are both reported. The plugin will also suggest updates to themes and plugins, as a new version may offer compatible code.

PHP7 Compatibility Report to move your WordPress site to PHP7

Query Monitor Plugin

Next, the second plugin to install is Query Monitor

As a result of installing Query Monitor, a new section with numbers will appear on the admin toolbar and will indicate the number of queries, timings errors, warnings, notices, and deprecated items for the current page. After installation, you will want to be aware of when the background of the Query Monitor turns red. When red the monitor is indicating there are errors is in the drop-down list. Click on Errors to see the section where there is a problem. You will get a nice listing of the PHP file and the exact line number located and needs to be fixed.

The next task is to fix all the error. Warnings are nice to have fixed but not necessary to run on PHP 7. So concentrate on the errors.

To Code or Not to Code – Making the Code Changes or Notifying Developers

Finally, once you have a list of the errors from both you must decide if you are going to fix the code yourself or let the developer know there is an issue. If you plan on upgrading the theme or plugins in the future it is best to let the developer know, wait for the fix and then upgrade. If you are not going to update then you are ready to move your WordPress site to PHP7 and will be making the code changes. Then upload the changed files and run the compatibility checker one more time to make sure all is looking good. At this point when there are no more errors reported your site is ready for the upgrade. At this point, you can then make a request for your web hosting service to upgrade your site to PHP7.

Examples of Issues

Luckily most issues found were warning though there were a few errors. There were several errors I saw quite a few times.

Below is error text from the scan are errors and examples of code changes made to fix the errors.

Ereg and MySQL extensions errors:

FOUND 2 ERRORS AFFECTING 2 LINES
31 | ERROR | Extension ‘mysql_’ is deprecated since PHP 5.5 and removed since PHP 7.0 – use mysqli instead.
82 | ERROR | Extension ‘mysql_’ is deprecated since PHP 5.5 and removed since PHP 7.0 – use mysqli instead.

Example:


  $connect = mysql_connect('localhost','root','');
  mysql_select_db('dbname');

Change to:


$connect = mysqli_connect('localhost', 'username', 'password', 'database');

Another Example:

FOUND 1 ERROR AFFECTING 1 LINE
32 | ERROR | eregi() – is deprecated since PHP 5.5 and removed since PHP 7.0


ereg('\.([^\.]*$)', $this->file_src_name, $extension);

Change to:


preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);

Conclusion

In conclusion, every site will be unique in what must be updated and some will not need any updates at all. These tools and tips should make it easier though to figure out which is the case and help you move to PHP 7 go smoothly.