2

I want to show a notice to the user and deactivate my plugin, using OOP code style, if the user has a PHP version less than 5.4.

The code works fine when I create my plugin using non-OOP. It shows an warning to the user and deactivate the plugin and prevents user from activating the plugin.

Working code is given below:

Non-OOP

// check for required php version and deactivate the plugin if php version is less.
 if ( version_compare( PHP_VERSION, '5.4', '<' )) {
add_action( 'admin_notices', 'show_notice', 100 );
function show_notice() { ?>
    <div class="error"> <p>
            <?php
            echo 'MyPluginName requires minimum PHP 5.4 to function properly. Please upgrade PHP version. The Plugin has been auto-deactivated.. You have PHP version '.PHP_VERSION;
            ?>
        </p></div>
    <?php
    if ( isset( $_GET['activate'] ) ) {
        unset( $_GET['activate'] );
    }
}

// deactivate the plugin because required php version is less.
add_action( 'admin_init', 'MyPluginName_deactivate_self' );
function MyPluginName_deactivate_self() {
    deactivate_plugins(plugin_basename( __FILE__ ) );
}
return;
}

However, this code does not work when I develop my plugin using Object Oriented Programing. I have tried the following:

Attempt #1

// check for required php version and deactivate the plugin if php version is less.
 if ( version_compare( PHP_VERSION, '5.4', '<' )) {
add_action( 'admin_notices', 'show_notice', 100 );
function show_notice() { ?>
    <div class="error"> <p>
            <?php
            echo 'MyPluginName requires minimum PHP 5.4 to function properly. Please upgrade PHP version. The Plugin has been auto-deactivated.. You have PHP version '.PHP_VERSION;
            ?>
        </p></div>
    <?php
    if ( isset( $_GET['activate'] ) ) {
        unset( $_GET['activate'] );
    }
}

// deactivate the plugin because required php version is less.
add_action( 'admin_init', 'MyPluginName_deactivate_self' );
function MyPluginName_deactivate_self() {
    deactivate_plugins(plugin_basename( __FILE__ ) );
}
return;
}


if ( ! class_exists('MyPluginClass') ) :
class MyPluginClass {
function __construct( ){
        //enqueue scripts/styles only for front-end
        add_action('template_redirect', [$this, 'user_enqueue_scripts']);
        //enqueue scripts and style only for admin panel
        add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );

        }
}

endif;
$MyPlugin = new MyPluginClass();

Attempt #2

if ( ! class_exists('MyPluginClass') ) :
    class MyPluginClass {
        function __construct( ){}

        public function check_php_version (){
            // check for required php version and deactivate the plugin if php version is less.
            if ( version_compare( PHP_VERSION, '5.4', '<' )) {
                add_action( 'admin_notices', 'show_notice', 100 );
                function show_notice() { ?>
                    <div class="error"> <p>
                            <?php
                            echo 'MyPluginName requires minimum PHP 5.4 to function properly. Please upgrade PHP version. The Plugin has been auto-deactivated.. You have PHP version '.PHP_VERSION;
                            ?>
                        </p></div>
                    <?php
                    if ( isset( $_GET['activate'] ) ) {
                        unset( $_GET['activate'] );
                    }
                }

                // deactivate the plugin because required php version is less.
                add_action( 'admin_init', 'MyPluginName_deactivate_self' );
                function MyPluginName_deactivate_self() {
                    deactivate_plugins(plugin_basename( __FILE__ ) );
                }
                return;
            }

        }

        public function init() {
            //enqueue scripts/styles only for front-end
            add_action('template_redirect', [$this, 'user_enqueue_scripts']);
            //enqueue scripts and style only for admin panel
            add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
        }

    }

endif;
$MyPlugin = new MyPluginClass();
$MyPlugin->check_php_version(); // show warning if php version is less than 5.4 and deactivate the plugin
$MyPlugin->init();// initialize the plugin.

Please let me know what I am doing wrong.

  • What errors are you getting when you try OOP? – Ethan Jinks O'Sullivan Aug 31 '16 at 17:52
  • Plugin gets activated even the php version is less than 5.4 and if I used short array syntax eg. [ ] anywhere in the code then a fatal error is also displayed upon activation of the plugin. But I just wanted to show warning and deactivate the plugin if user has php version less than 5.4. By the way, let me try your solution. I hope it may work. I will let you know. And Thank you very much for answering. – Kamal Ahmed Aug 31 '16 at 19:55
  • Yeah, that was a dated question before I provided the solution. Forgot to delete it. – Ethan Jinks O'Sullivan Aug 31 '16 at 20:26
2

Here's my bare-bones template that I use when creating a plugin with OOP. Feel free to modify to your liking.

Template

if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if ( !class_exists( 'MyPluginName' ) ) {

    class MyPluginName {

        public function __construct() { // Call your actions/filters here
            add_action( 'tag', array( $this, 'plgn_abbr_function' ), 10, 1 );
        }

        // Begin functions here
        public function plgn_abbr_function() {
            # Code here...
        }
    }
}

if ( class_exists( 'MyPluginName' ) ) { // Instantiate the plugin class
    global $plgn_abbr;
    $plgn_abbr = new MyPluginName();
}

A few things I immediately noticed from skimming through your attempts:

  1. You'd want to call your add_actions under the __construct() and have your functions places outside of that but you'll need to change the layout of it as well.
  2. You need to update how your add_action layout

    add_action( 'admin_notices', 'show_notice', 100 );

    This structure will not work when utilizing a class, it should be:

    add_action( 'admin_notices', array( $this, 'show_notice' ), 100 );

  3. You'd need to append public before all of your functions like so:

    public function your_code()

Solution

Here's an update to your code utilizing the template that I gave you above, it's been tested and works on my end:

if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if ( !class_exists( 'MyPluginName' ) ) {

    class MyPluginName {

        public function __construct() {
            // check for required php version and deactivate the plugin if php version is less.
             if ( version_compare( PHP_VERSION, '5.4', '<' ) ) {
                add_action( 'admin_notices', array( $this, 'show_notice' ), 100 );
                add_action( 'admin_init', array( $this, 'MyPluginName_deactivate_self' ) );
                return;
            }
        }

        public function show_notice() {
            ?>
            <div class="error">
                <p><?php echo 'MyPluginName requires minimum PHP 5.4 to function properly. Please upgrade PHP version. The Plugin has been auto-deactivated.. You have PHP version '.PHP_VERSION; ?></p>
            </div>
            <?php
            if ( isset( $_GET['activate'] ) ) {
                unset( $_GET['activate'] );
            }
        }
        public function MyPluginName_deactivate_self() {
            deactivate_plugins( plugin_basename( __FILE__ ) );
        }
    }
}

if ( class_exists( 'MyPluginName' ) ) { // Instantiate the plugin class
    global $plgn_abbr;
    $plgn_abbr = new MyPluginName();
}
  • This codes works if no php 5.4 features eg. short array syntax [ ] etc is not used anywhere in the code. This is the main problem. I wanted to stop the script for running further if php version is less and deactivate the plugin. But it shows fatal errors. for example, If I add a test function on the bottom of the class like this: public function test(){ return [1,3,4]; } Then it will throw a fatal error on plugin activation for using short array syntax which is a php > 5.4 feature. Can you please help me solve this? – Kamal Ahmed Aug 31 '16 at 20:13
  • That's an entirely separate question which I recommend creating a new one instead of adding it onto here. Since you originally asked how to display a warning and deactivate the plugin if PHP version > 5.4. I'd say this question has been solved. – Ethan Jinks O'Sullivan Aug 31 '16 at 20:27
  • Problem is not solved. It is obvious that I will use PHP 5.4 feature in my plugin files, So, I wanted to warn user and deactivate the plugin instantly on plugin activation so that no error is shown. But what is the use of showing warning to a user if no php 5.4 feature can not be used in the rest of the code? So, it is very easy to understand that code has php 5.4 features, so if user does not use php 5.4, then plugin should be deactivated with a notice given to the user. That is it. and your solution could not do this. It still shows error when I use it with rest of my code. I am Sorry. – Kamal Ahmed Aug 31 '16 at 21:02
  • by the way, I got a temporary fix and your code also helped. and yes, you did a good job and your answer was to the point though it did not solve all my problem but I am satisfied and marking it as accepted. Thanks for your help. – Kamal Ahmed Aug 31 '16 at 21:17
  • @KamalAhmed Glad to help. If you want to edit my answer with your modified solution, free free to do so if someone else were to visit this question in the future. – Ethan Jinks O'Sullivan Aug 31 '16 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.