【问题标题】:Disable Message which Appears when Activating WordPress Plugin禁用激活 WordPress 插件时出现的消息
【发布时间】:2012-08-28 23:44:31
【问题描述】:

当用户的 PHP 版本不足时,我使用下面的代码来停用插件本身。一个问题是出现黄色消息框,说明该插件已激活,尽管此功能成功拒绝了该插件。那么有没有办法不显示黄色消息?

function Plugin_Requirements() {
    global $wp_version;
    $plugin = plugin_basename( __FILE__ );
    $plugin_data = get_plugin_data( __FILE__, false );
    $numPHPver='5.1.2';     
    $strMsg .= $plugin_data['Name'] . ': ' . __('requires PHP')  . ' ' . $numPHPver . __(' or higher. Your PHP version is : ') . phpversion() . __('. Deactivating the plugin.') . '<br />';

    if ( version_compare(phpversion(), $numPHPver, "<" ) ) {
        echo '<div class="error"><p>' . $strMsg . '</p></div>';
        deactivate_plugins( $plugin );
    }   
}
add_action( 'admin_init', 'Plugin_Requirements' );

【问题讨论】:

  • 您是否检查过插件是否被禁用 bij 在 WP_OPTIONS 表中查找它的值?
  • 不确定你的意思。它使用phpversion() 检查版本。
  • 你在哪里添加了未设置的?在 index.php 中添加
  • 编辑核心不是我想要的。我希望能够作为插件的一部分。我尝试将 unset() 与不同的钩子(包括 init、admin_int 和 plugins_loaded)一起使用,但它们似乎都不起作用。

标签: php wordpress


【解决方案1】:

您可以取消设置触发消息的$_GET 变量:

if ( version_compare(phpversion(), $numPHPver, "<" ) ) {
    echo '<div class="error"><p>' . $strMsg . '</p></div>';
    deactivate_plugins( $plugin );
    unset($_GET['activate']);
}

我认为更好的方法是不允许您的插件被激活,方法是退出(或抛出错误)它的激活钩子。 Wordpress 将显示一条错误消息,说明由于致命错误而无法激活插件,但这似乎适合这里。

function my_activation_hook() {
    $required_php_version = '5.1.2';

    if (version_compare(PHP_VERSION, $required_php_version, '<')) { 
        $plugin_data = get_plugin_data(__FILE__, false);
        $message = $plugin_data['Name'] . ' ' . __('requires PHP')  . ' ' . $required_php_version . __(' or higher. Your PHP version is ') . phpversion() . '.';
        echo "<p>{$message}</p>";
        exit;
    } 
} 
register_activation_hook(__FILE__, 'my_activation_hook');

【讨论】:

  • 有趣。我不想显示消息“无法激活插件,因为它触发了致命错误。”有可能吗?
  • 我尝试使用admin_int 钩子和register_activation_hook() 取消设置$_GET,但它仍然显示致命错误消息。我不确定我是否明白你所说的。你的意思是unset($_GET['action']);?但这也不会禁用该消息。
  • 不,如果您退出激活挂钩,我认为您无法隐藏致命错误消息。
  • 我明白了。让我打开这个问题一段时间,看看其他人是否有解决方案。谢谢你的回答。
【解决方案2】:

要做到这一点有点棘手。 RichardM 的答案非常接近,但这是我发现的作品,没有显示致命错误或必须使用wp_die()。诀窍是在您检查了插件的要求之后调用register_activation_hook() 。如果您不这样做,那么 WordPress 会愉快地激活插件并生成“已激活插件”。在您检查您的要求之前发送消息。

我还喜欢在我的消息中使用瞬变,这样我就可以随时显示我的错误消息。当您需要与用户交流多条消息时,它会更有用。

示例代码:

<?php
/**
 * Plugin Name: My Plugin
 * Description: A sample plugin activation call without generating "Plugin activated."
 *     message if requirements aren't met.
 * Author: Slicktrick
 * Version: 1.0.0
 * Requires at least: 4.0
 */

add_action( 'admin_init', 'check_requirements_for_my_plugin' );
add_action( 'admin_notices', 'show_notices_for_my_plugin' );

function check_requirements_for_my_plugin() {
    // Check that we are using PHP 5.1.2 or greater.
    // Modify this to use whatever checks you need.
    $phpversion = '5.1.2';
    if ( version_compare(phpversion(), $phpversion, "<" ) ) {
        // deactivate and remove flag to activate plugin
        deactivate_plugins( plugin_basename( __FILE__ ) );
        if ( isset( $_GET['activate'] ) ) {
            unset( $_GET['activate'] );
        }

        // Create an error message and store it as a transient
        // Transient is set to automatically expire after 120 seconds
        // This prevents the message from lingering indefinitely
        $error_message = "PHP version $phpversion required. My Plugin was not activated.";
        set_transient( 'my_plugin_activation_error_message', $error_message, 120 );
    } else {
        // Here's the trick, we register our activation hook
        // now that we know the requirements are met!
        register_activation_hook( __FILE__, 'activate_my_plugin' );
    }
}

function show_notices_for_my_plugin() {
    // We attempt to fetch our transient that we stored earlier.
    // If the transient has expired, we'll get back a boolean false
    $message = get_transient( 'my_plugin_activation_error_message' );

    if ( ! empty( $message ) ) {
        echo "<div class='notice notice-error is-dismissible'>
            <p>$message</p>
        </div>";
    }
}

function activate_my_plugin() {
    // Add activation code here
}

// Add the rest of your plugin code here

check_requirements_for_my_plugin() 函数将在每次执行 admin_init 时运行。虽然一旦插件被激活,这似乎是一个多余的检查,但想想如果有人更改托管服务提供商或移动到不同的服务器会发生什么。新的服务器环境可能不支持您的插件,如果 WordPress 数据库被直接复制(很可能),用户最终可能会遇到各种错误和功能损坏。所以每次调用我们的check_requirements_for_my_plugin() 函数是必要的。

如果您不想在您的插件无法激活时向用户显示任何消息(您为什么要让您的用户不知道您的插件为什么没有激活???),那么就可以去掉下面三行代码加上整个show_notices_for_my_plugin()函数:

// at the top of the file
add_action( 'admin_notices', 'show_notices_for_my_plugin' );

// from the check_requirements_for_my_plugin() function
$error_message = "PHP version $phpversion required. My Plugin was not activated.";
set_transient( 'my_plugin_activation_error_message', $message, 120 );

参考资料:

  1. WordPress Codex - Admin Notices
  2. WordPress Codex - Transients API

更新

基于来自 WordPress 首席开发人员的 answercomment,似乎更好的方法是让您的插件保持激活状态但禁用所有功能。一旦满足要求,您就可以使用其他检查来执行您需要执行的任何安装任务。这可以通过以下示例代码轻松完成:

add_action( 'plugins_loaded', 'check_requirements_for_my_plugin' );

function check_requirements_for_my_plugin() {
    // Do whatever checking needed here
    if ( ! $requirements_met ) {
        add_action( 'admin_notices', 'show_requirements_notice_for_my_plugin' );
        return; // Exit your function
    }
    // Register all other hooks here since you know requirements are
    // met if you reach this point
    add_action( 'admin_init', 'maybe_update_my_plugin' );
}

function show_requirements_notice_for_my_plugin() {
    // I'd expand this to add a list of needed requirements
    echo '<div class="notice notice-error">'
        . "My-Plugin functionality is disabled because the requirements are not met."
        . '</div>';
}

function maybe_update_my_plugin() {
    // Check to see if update/installation tasks need to be performed
}

此方法允许您将通知保留在用户面前,以便他们对其采取行动。当他们尝试激活插件时,我的原始答案会显示一条通知,但是一旦加载新页面,通知就会消失,因为此时插件不再处于活动状态。

【讨论】:

    【解决方案3】:
    function _my_plugin_php_warning() {
        echo '<div id="message" class="error">';
        echo '  <p>My Plugin requires at least PHP 5.  Your system is running version ' . PHP_VERSION . ', which is not compatible!</p>';
        echo '</div>';
    }
    
    function deactivate_plugin_conditional() {
    
        if ( version_compare( PHP_VERSION, '5.2', '<' ) ) {
            $plugin = plugin_basename(__FILE__);
    
            if ( is_plugin_active($plugin) ) {
                deactivate_plugins($plugin);    
            }   
            add_action('admin_notices', '_my_plugin_php_warning');
    
        } else {
            //Load classes
        }
    }
    
    add_action( 'admin_init', 'deactivate_plugin_conditional' );
    

    这只会在没有错误发生时激活插件。如果插件已经激活,它将被禁用。希望这对你有用。请告诉我结果如何。

    【讨论】:

    • wp_die() 在不同页面中显示消息。所以用户必须回到插件列表页面。我只想在页面顶部显示一个红色警告框。
    • 我不习惯直接操作数据库。 global $wpdb; $myrows = $wpdb-&gt;get_results( "SELECT * FROM wp_options WHERE option_name = 'active_plugins'" ); print_r($mywors); 什么也没说。我想看看一些有效的例子。
    • 我尝试了您的更新代码,但它并没有禁用黄色通知框。它还需要禁用插件。
    • 编辑核心不是明智的选择,因为当WordPress升级时,修改的行将消失。将其作为插件也变得毫无意义。所以这不是我要找的。不过,我感谢您的时间和努力。
    • 编辑核心肯定是不好的做法,但到​​目前为止我能找到的唯一选择。我要另辟蹊径。
    【解决方案4】:

    get_plugin_data()

    函数只能在 WordPress 管理员中使用,而不是使用 get_plugin_data() 您可以在插件中定义插件名称和插件版本的常量,看看这个线程 http://wordpress.org/support/topic/php-fatal-error-call-to-undefined-function-get_plugin_data

    【讨论】:

      【解决方案5】:

      如果您根本不希望出现该消息,请转到 php 页面并在页面顶部的开始 php 标记之后立即添加 error_reporting(0)。

      【讨论】:

        【解决方案6】:

        我遇到了类似的问题,涉及register_activation_hook() 和全局变量 $wp_version:每次激活或停用我的插件时,我都会在 debug.log 文件中收到以下消息:

        PHP 注意:未定义变量:wp_version (...)

        当我将 $wp_version 替换为 get_bloginfo('version') 时,问题解决了

        【讨论】:

          【解决方案7】:

          目前似乎无法将其作为插件的一部分。

          【讨论】:

            猜你喜欢
            • 2017-11-11
            • 2016-11-09
            • 2015-09-06
            • 2012-07-16
            • 2017-01-17
            • 1970-01-01
            • 2021-06-19
            • 2015-04-26
            • 2022-01-21
            相关资源
            最近更新 更多