OOP: Display warning and deactivate the plugin if PHP version is less than 5.4
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.
Topic deactivation plugin-development hooks plugins Wordpress
Category Web