In this quick tip, we’ll learn about how to ensure our WordPress plugin runs and executes code only when certain conditions are met, mainly compatibility conditions such as having a minimal PHP version to be supported, and a WordPress version as well.

I wrote a PHP helper class yesterday and its aim is to wrap your main plugin code in it which will make sure it will run only when those conditions you state are met, and throws a nice error message in the admin if not, with the option to deactivate the plugin or keep it running as incompatible.

This class can work with PHP versions down until 5.0, even though the minimum WordPress supporting version is 5.2.

This class is available for download on Github: https://github.com/elhardoum/wp-php-compat-check, and it is documented there as well https://github.com/elhardoum/wp-php-compat-check/blob/master/readme.md.

// just an example
CompatCheckWP::check(array(
    'php_version' => 7.0,
    'deactivate_incompatible' => true,
    'wp_version' => 4.2,
))->then(function(){
    function my_plugin_function_a( $content ) {
        // [...]
    }


    function my_plugin_function_b($query) {
        // [...]
    }

    // [...] other plugin code
});

Digital Ocean

Cheap Cloud SSD Hosting

Get a VPS now starting at $5/m, fast and perfect for WordPress and PHP applications

Sign Up with $10 Credit