14

I have a plugin that I do not want to be activated if it doesn't meet a certain WP version number then show error message in admin_notices action hook. As far as I have researched, the code below is the best that I can achieve this goal:

$wp_version = get_bloginfo('version');
if ( $wp_version < 4.5 ) {
    add_action( 'admin_init', 'deactivate_plugin_now' );
    add_action( 'admin_notices', 'errormsg' ) );
}

public function deactivate_plugin_now() {
    if ( is_plugin_active('myplugin/myplugin.php') ) {
        deactivate_plugins('myplugin/myplugin.php');
    }
}

public function errormsg () {
    $class = 'notice notice-error';
    $message = __( 'Error you did not meet the WP minimum version', 'text-domain' );
    printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message );
}

But I think I am still doing it wrong because I'm getting the plugin activated message at the same time with the error notice that I assigned.

Stop a plugin in the activation process when a certain WP version is not met

What would be the proper action hook / filter to properly stop the plugin activation process so I will only get the error message?

1
  • Plese @CarlAlberto consider my answer.
    – prosti
    Commented Jan 5, 2017 at 17:15

5 Answers 5

11

I may be late to this party, but to stop plugin activation and have WordPress show an error message where the admin notices go, I simply output an error message and terminate execution. This has the added advantage of playing nice with wp-cli:

Plugin activation failed

Example:

class testPlugin() {

  ...

   static function activate() {

   //[do some stuff here]

   if ($error) {
      die('Plugin NOT activated: ' . $error);
   }

}

register_activation_hook( __FILE__, array( 'testPlugin', 'activate' ));
1
  • 2
    Which version are you tested this with? I'm developing with docker and 6.2.2-php8.1 WP image but i'm just getting the generic 'Plugin could not be activated because it triggered a fatal error.' without the die message appended.
    – fudo
    Commented Nov 30, 2023 at 9:10
3

Essentialy, you can not do it the way you want. All wordpress form are redirecting after completing their process and do not generate output by themself and therefor the error message is generated on a different page request. If your plugin will not be active at that point, there will be no message displayed. Further complication is that plugins might be activated by Ajax.

An ugly but working way is to fail the activation by generating an php error, or IIRC any output will do, so you can just output something like "version mismatch" in the plugin activation hook, which will be displayed in the error output box that is being displayed when activation fails.

Something to think about: people might include your plugin files, or somehow activate it forcefully by bypassing the UI, or using wp-cli. depending on the reasons you want to fail the activation, just having the error message might be better than trying to fail the activation

0
3

How about this code to suppress hello.php (Hello Dolly) if WP < 8.5:

 add_action( 'activate_plugin', '_20170113_superess_activate' , 10, 2);

 function _20170113_superess_activate($plugin, $network_wide){
      global $wp_version;

      if ( $wp_version < 8.5 && 'hello.php' == $plugin ) {
        error_log( 'WordPress need to be at least 8.5' );      
        $args = var_export( func_get_args(), true );
        error_log( $args );
        wp_die( 'WordPress need to be at least 8.5 to activate this plugin' );
      }
 }

I think this is good because it doesn't force you to create admin notices. You will simply get the feedback about the reason for abortion of the plugin install.

I added this code for HelloDolly, but you will need to adjust.

2
  • 1
    thanks, using this method to prevent users from activating certain plugins Commented Nov 19, 2021 at 15:11
  • 1
    ah, it takes sometimes so long 4 years for the code snippet to help someone. :) nice.
    – prosti
    Commented Nov 22, 2021 at 9:40
1

Since you updated the title I need to provide the another answer, the previous one please don't ignore because it is even more simple than this one.

add_action( 'admin_notices', 'my_plugin_admin_notices' );

function my_plugin_admin_notices() {
    if ( ! is_plugin_active( 'hello.php' ) && isset( $_GET['customhello'] ) ) {
        echo '<div class="error"><p>WordPress need to be at least 8.5 to activate this plugin</p></div>';
    }
}

add_action( 'activate_plugin', '_20170113_superess_activate', 10, 2 );

function _20170113_superess_activate( $plugin, $network_wide ) {
    global $wp_version;
    if ( $wp_version < 8.5 && 'hello.php' == $plugin ) {
        $redirect = self_admin_url( 'plugins.php?customhello=1' );
        wp_redirect( $redirect );
        exit;
    }
}

enter image description here

Please note that when you try to activate the plugin you interact with plugins.php file. In there you have a big switch of actions that you can run.

And in case you have the error set $_GET['error'] at the moment you have just the predefined error messages — you cannot create your own custom message. This is why I proposed you the solution like this.

enter image description here

One may propose the custom messages as admin notices to the WordPress core in cases plugins do not meet some requirements.

1
  • 5
    This does not work. If the plugin is not active, WP won't execute the scripts in the file, therefore, when hitting the redirect url, the function hooked to admin_notices isn't fired.
    – JakeParis
    Commented Jun 6, 2017 at 15:12
1

I know I am too late to post the reply on this but it may help someone. You could just unset the $_GET variable which triggers the message:

$wp_version = get_bloginfo('version');
if ( $wp_version < 4.5 ) {
    add_action( 'admin_init', 'deactivate_plugin_now' );
    add_action( 'admin_notices', 'errormsg' ) );
}

public function deactivate_plugin_now() {
    if ( is_plugin_active('myplugin/myplugin.php') ) {
        deactivate_plugins('myplugin/myplugin.php');
        unset($_GET['activate']);
    }
}

public function errormsg () {
    $class = 'notice notice-error';
    $message = __( 'Error you did not meet the WP minimum version', 'text-domain' );
    printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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