0

I've creating a theme options page at the moment and was playing around with this function deactivate_plugins() I'm running wordpress 3.8.3 so running this code in my plugin should uninstall it.

Questions

1.Am I using this function incorrectly? 2. Because the version 4.0 of Wordpress doesn't exist does the logic not work?

 register_activation_hook( __FILE__, 'dw_deactivate_theme_options' ); // activate hook

    function dw_deactivate_theme_options() {
        if ( version_compare( get_bloginfo('version'), '4.0', '<') )  { // check the version
            deactivate_plugins( basename( __FILE__ ) ); // deactivate this plugin if the version is lower then 3.8
        }
    }

Full plugin code

<?php

/*
Plugin Name: Theme Settings
Plugin URI: xxx
Description: This Plugin will generate all of the theme options, Social Media, Logo, Google analytics.
Version: 0.0.1
Author: xxx
Author URI: xxx
License: GPL2
*/

/*
Function Reference 
plugins_url() - Full plugins directory URL (for example,http://example.com/wp - content/plugins )
register_activation_hook(); Hook to add settings before plugin activation 2nd parem is callback function


*/

//Preset plugin functions

register_activation_hook( __FILE__, 'dw_deactivate_theme_options' ); // activate hook

function dw_deactivate_theme_options() {
    if ( version_compare( get_bloginfo('version'), '4.0', '<') )  { // check the version
        deactivate_plugins( basename( __FILE__ ) ); // deactivate this plugin if the version is lower then 3.8
    }
}

add_action('admin_menu', 'dw_theme_options_create_menu');

function dw_theme_options_create_menu() {
    //create custom top-level menu
    add_menu_page('Theme Options', 'Theme Settings', 'manage_options', __FILE__, 'dw_theme_options_cb');
}

function dw_theme_options_cb() {
    echo '<img src"' .plugins_url( 'img/theme_icon.png', __FILE__ ) . '">'; // ?>
        <h2>Theme Settings</h2>
    <?php echo get_bloginfo('version'); ?>
<?php } ?>
0

The problem is not version_compare, the problem is that register_activation_hook is just a shortcut for

add_action( 'activate_' . $file, $function );

where $file is plugin_basename( __FILE__ ); and __FILE__ is your main plugin file absolute path.

So, what your code does is to call dw_deactivate_theme_options() when the action 'activate_' . $plugin is fired.

But this action is fired before the plugin is activated i.e. before the plugin is added to the option 'active_plugins'.

In short you are trying to deactivate a plugin that is not yet active and so your function does nothing.

There are 2 easy ways to avoid a plugin is activated under some conditions inside the function registered with register_activation_hook:

  1. the first is do a redirect:

    function dw_deactivate_theme_options() {
      if ( version_compare( get_bloginfo('version'), '4.0', '<') )  {
        global $status, $paged, $s;
        $url = self_admin_url( "plugins.php?plugin_status=$status&paged=$page&s=$s" );
        wp_redirect( $url );
        exit();
      }
    }
    

    this works, but users just see the page being reloaded and plugin not activated, but without any error message... it's not very intuitive

  2. the second, probably better, way is die inside the function, in this way the plugin is not activated and you can give a feedback to users putting a message into die function:

    function dw_deactivate_theme_options() {
      if ( version_compare( get_bloginfo('version'), '4.0', '<') )  {
        $message = "Impossible activate Theme Settings plugin because ";
        $message .= "WordPress version is lower than 4.0";
        die( $message );
      }
    }
    

    In this way plugin is not activated and users see a message, even if WordPress will say that your plugin triggered a fatal error..

Check WP version before activate a plugin, and trigger error if check fail

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.