在PHP版本不足的情况下中止插件的最佳方法是什么?

时间:2012-12-13 作者:Geert

您编写了一个需要PHP 5.1的插件。有人试图用PHP 4将其安装到服务器上。您如何以安全和用户友好的方式处理此问题?

4 个回复
最合适的回答,由SO网友:Geert 整理而成

/**
 * Plugin Name: Foo
 */

// Check for required PHP version
if ( version_compare( PHP_VERSION, \'5.1\', \'<\' ) )
{
    exit( sprintf( \'Foo requires PHP 5.1 or higher. You’re still on %s.\', PHP_VERSION ) );
}

// The rest of your plugin code follows
我不确定这是从哪个WP版本发生的,但在3.5中插件实际上无法激活,错误消息会在管理员中显示给用户,这很整洁。

但是,错误消息未翻译。为了做到这一点,您必须在exit 呼叫

SO网友:Chris_O

此函数和激活挂钩可防止插件激活,并允许您检查PHP和WordPress的最低版本。

register_activation_hook( __FILE__, array( \'Your_Plugin_Class_Name\', \'activate\' ) );

/**
  * Plugin Activation hook function to check for Minimum PHP and WordPress versions
  * @param string $wp Minimum version of WordPress required for this plugin
  * @param string $php Minimum version of PHP required for this plugin
  */
 function activate( $wp = \'3.1\', $php = \'5.2.4\' ) {
    global $wp_version;
    if ( version_compare( PHP_VERSION, $php, \'<\' ) )
        $flag = \'PHP\';
    elseif
        ( version_compare( $wp_version, $wp, \'<\' ) )
        $flag = \'WordPress\';
    else
        return;
    $version = \'PHP\' == $flag ? $php : $wp;
    deactivate_plugins( basename( __FILE__ ) );
    wp_die(\'<p>The <strong>Insert PLugin Name Here</strong> plugin requires\'.$flag.\'  version \'.$version.\' or greater.</p>\',\'Plugin Activation Error\',  array( \'response\'=>200, \'back_link\'=>TRUE ) );
}

SO网友:onetrickpony

您可以激活它并显示错误消息:

// if PHP version is lower than 5.1
if(version_compare(PHP_VERSION, \'5.1\') < 0){

  // show a message inside the dashboard
  if(is_admin()){

    function my_plugin_notice(){      
      ?>
      <div class="error below-h2">
        <p>
        <?php
          printf(__(\'The abc plugin requires at least PHP 5.1. You have %s\'), PHP_VERSION);
         ?>
        </p>
      </div>
      <?php
    }

    add_action(\'admin_notices\', \'my_plugin_notice\');

  }

  // stop here and do nothing further
  return;  
}

// if PHP version is equal or higher than 5.1
require dirname(__FILE__) . \'/php51code.php\';
也有可能在返回语句之前以编程方式停用它。。。

SO网友:Joshua Nelson

我知道这是一个古老的问题,但对于那些寻求好的解决方案的人来说,Gary Pendergast有一条很好的路线,涵盖了其他答案中提到的一些基础(参见他的帖子here, 我已经更新了下面的代码来检查PHP版本,但您几乎可以将其用于任何检查):

//  In this example, only allow activation on WordPress 3.7 or higherclass 
MyPlugin {
function __construct() {
    add_action( \'admin_init\', array( $this, \'check_version\' ) );

    // Don\'t run anything else in the plugin, if we\'re on an incompatible WordPress version
    if ( ! self::compatible_version() ) {
        return;
    }
}

// The primary sanity check, automatically disable the plugin on activation if it doesn\'t// meet minimum requirements.static
function activation_check() {
    if ( ! self::compatible_version() ) {
        deactivate_plugins( plugin_basename( __FILE__ ) );
        wp_die( __( \'My Plugin requires PHP 5.1 or higher!\', \'my-plugin\' ) );
    }
}

// The backup sanity check, in case the plugin is activated in a weird way,
// or the versions change after activation.
function check_version() {
    if ( ! self::compatible_version() ) {
        if ( is_plugin_active( plugin_basename( __FILE__ ) ) ) {
            deactivate_plugins( plugin_basename( __FILE__ ) );
            add_action( \'admin_notices\', array( $this, \'disabled_notice\' ) );

            if ( isset( $_GET[\'activate\'] ) ) {
                unset( $_GET[\'activate\'] );
            }
        }
    }
}

function disabled_notice() {
    echo \'<strong>\' . esc_html__( \'My Plugin requires PHP 5.1 or higher!\', \'my-plugin\' ) . \'</strong>\';
}

static function compatible_version() {
    if ( version_compare(PHP_VERSION, \'5.1\', \'<\') ) {
        return false;
    }

    // Add sanity checks for other version requirements here

    return true;
}
}
global $myplugin;
$myplugin = new MyPlugin();
register_activation_hook( __FILE__, array( \'MyPlugin\', \'activation_check\' ) );
我还将上述代码保存在gist.

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register