When developing a WordPress plugin, ensuring it runs on your client’s server without breaking their site is super important. One of the easiest and most effective ways to prevent plugin-related fatal errors is by checking for the required minimum PHP version before your plugin’s functionality loads.
If you skip this check, your plugin might fail silently—or worse, take down the entire site due to incompatibility.
Why Are PHP Version Checks Important?
Recently, while working on an add-on for the BWL Advanced FAQ Manager WordPress plugin, I ran into a real-world scenario highlighting this issue.
The add-on used Composer to manage dependencies, some of which required PHP 8.2. My local development environment (PHP 8.2.27) handled everything without any error. However, once I deployed the add-on to the production server, the site went down with a fatal error related to PHP version incompatibility.
My hosting provider, Hostinger, has simplified the process of upgrading PHP versions through hPanel. Upgrading the PHP version solved the problem, but a better approach is to let your plugin identify PHP version issues and notify the user proactively within the WordPress dashboard.
Check PHP version and show admin notice
Here’s how you can check for the required PHP version and display a clear admin notice if the client’s server does not meet your plugin’s requirements.
Define the Required PHP version.
Start by defining the minimum PHP version your plugin requires. For example:
define( 'BAFIEADDON_MIN_PHP_VERSION', '8.2.10' );
Compare Current Server PHP Version
Use PHP’s version_compare() function to check if the server’s PHP version meets your requirement:
if ( version_compare( PHP_VERSION, BAFIEADDON_MIN_PHP_VERSION, '<' ) ) { add_action( 'admin_notices', 'bafieaddon_php_version_notice' ); return; }
Display the Admin Notice
Create a function to display a clear error notice in the WordPress admin area:
function bafieaddon_php_version_notice{ $message = sprintf( /* translators: 1: Plugin name, 2: Required PHP version, 3: Current PHP version */ esc_html__( '%1$s requires PHP version %2$s or higher. You are running PHP version %3$s. Please upgrade your PHP version.', 'baf-import-export-addon' ), '<strong>BWL Advanced FAQ Import/Export Addon</strong>', BAFIEADDON_MIN_PHP_VERSION, PHP_VERSION ); printf( '<div class="notice notice-error"><p>%s</p></div>', wp_kses_post( $message ) ); };
Checking the PHP version before executing your plugin’s functionality is a small step that saves you (and your clients) from headaches and broken sites. It’s also a best practice for ensuring a professional, user-friendly experience for your plugin users.
Happy coding! 🚀