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?

5 s
5

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' ));

Leave a Reply

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