如果用户的PHP版本低于5.4
.
当我使用非OOP创建插件时,代码运行良好。它向用户显示警告并停用插件,并阻止用户激活插件。
工作代码如下:
非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;
}
然而,当我使用
Object Oriented Programing. 我尝试了以下方法:
尝试#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();
尝试#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.
请让我知道我做错了什么。
最合适的回答,由SO网友:Ethan O\'Sullivan 整理而成
这是我在使用OOP创建插件时使用的基本模板。您可以根据自己的喜好随意修改。
模板
if ( !defined( \'ABSPATH\' ) ) exit; // Exit if accessed directly
if ( !class_exists( \'MyPluginName\' ) ) {
class MyPluginName {
public function __construct() { // Call your actions/filters here
add_action( \'tag\', array( $this, \'plgn_abbr_function\' ), 10, 1 );
}
// Begin functions here
public function plgn_abbr_function() {
# Code here...
}
}
}
if ( class_exists( \'MyPluginName\' ) ) { // Instantiate the plugin class
global $plgn_abbr;
$plgn_abbr = new MyPluginName();
}
通过浏览您的尝试,我立即注意到了以下几点:
您想给您的add_actions
在__construct()
并将您的功能放置在这之外,但您还需要更改它的布局您需要更新add_action
布局
add_action( \'admin_notices\', \'show_notice\', 100 );
当使用
class
, 它应该是:
add_action( \'admin_notices\', array( $this, \'show_notice\' ), 100 );
您需要附加
public
在所有这样的功能之前:
public function your_code()
解决方案
这里是对您的代码的更新,使用了我在上面提供的模板,它已经过测试,并且在我这方面有效:if ( !defined( \'ABSPATH\' ) ) exit; // Exit if accessed directly
if ( !class_exists( \'MyPluginName\' ) ) {
class MyPluginName {
public function __construct() {
// 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\', array( $this, \'show_notice\' ), 100 );
add_action( \'admin_init\', array( $this, \'MyPluginName_deactivate_self\' ) );
return;
}
}
public 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\'] );
}
}
public function MyPluginName_deactivate_self() {
deactivate_plugins( plugin_basename( __FILE__ ) );
}
}
}
if ( class_exists( \'MyPluginName\' ) ) { // Instantiate the plugin class
global $plgn_abbr;
$plgn_abbr = new MyPluginName();
}