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.
What would be the proper action hook / filter to properly stop the plugin activation process so I will only get the error message?