🎉 Get the Insurfin - Insurance Service HTML5 Template for FREE - Offer Ends Soon!
Show a Minimum PHP Version Notice in Your WordPress Plugin

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! 🚀

Subscribe to BlueWindLab newsletter for practical plugin development tips, web development, and WordPress dev best practices every week.

Total 0 Votes
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*

Back To Top
Premium WordPress Themes and Plugins By xenioushk
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.