如何将插件排除在自动更新之外?

时间:2014-01-27 作者:David

有一个选择加入过滤器,允许我网站上的所有插件接收自动更新:

add_filter( \'auto_update_plugin\', \'__return_true\' );

我喜欢这个功能,但我不希望所有插件都自动更新。如何允许一些插件自动更新,同时排除那些我想手动更新的插件?

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

而不是使用函数中问题的代码。php,将其替换为:

/**
 * Prevent certain plugins from receiving automatic updates, and auto-update the rest.
 *
 * To auto-update certain plugins and exclude the rest, simply remove the "!" operator
 * from the function.
 *
 * Also, by using the \'auto_update_theme\' or \'auto_update_core\' filter instead, certain
 * themes or Wordpress versions can be included or excluded from updates.
 *
 * auto_update_$type filter: applied on line 1772 of /wp-admin/includes/class-wp-upgrader.php
 *
 * @since 3.8.2
 *
 * @param bool   $update Whether to update (not used for plugins)
 * @param object $item   The plugin\'s info
 */
function exclude_plugins_from_auto_update( $update, $item ) {
    return ( ! in_array( $item->slug, array(
        \'akismet\',
        \'buddypress\',
    ) ) );
}
add_filter( \'auto_update_plugin\', \'exclude_plugins_from_auto_update\', 10, 2 );
这段代码也可以轻松地进行调整,以自定义主题和核心更新。

Wordpress 3.8.2中添加了插件和主题更新统计信息(27905). 上述函数使用slug来识别插件,但您可以使用对象的任何信息(以$项为单位):

[id] => 15
[slug] => akismet
[plugin] => akismet/akismet.php
[new_version] => 3.0.0
[url] => https://wordpress.org/plugins/akismet/
[package] => https://downloads.wordpress.org/plugin/akismet.3.0.0.zip
对于Wordpress 3.8.1及以下版本,请改用此功能:

function exclude_plugins_from_auto_update( $update, $item ) {
    return ( ! in_array( $item, array(
        \'akismet/akismet.php\',
        \'buddypress/bp-loader.php\',
    ) ) );
}
add_filter( \'auto_update_plugin\', \'exclude_plugins_from_auto_update\', 10, 2 );
道具转到@WiseOwl9000,指出WP 3.8.2的变化

SO网友:WiseOwl9000

注意:从wordpress 3.8.2开始,传递给此函数的插件项的类型已经更改,现在它是一个对象。

/**
 * @package Plugin_Filter
 * @version 2.0
 */
/*
Plugin Name: Plugin Filter
Plugin URI: http://www.brideonline.com.au/
Description: Removes certain plugins from being updated. 
Author: Ben Wise
Version: 2.0
Author URI: https://github.com/WiseOwl9000
*/

/**
 * @param $update bool Ignore this it just is set to whether the plugin should be updated
 * @param $plugin object Indicates which plugin will be upgraded. Contains the directory name of the plugin followed by / followed by the filename containing the "Plugin Name:" parameters.  
 */
function filter_plugins_example($update, $plugin)
{
    $pluginsNotToUpdate[] = "phpbb-single-sign-on/connect-phpbb.php";
    // add more plugins to exclude by repeating the line above with new plugin folder / plugin file

    if (is_object($plugin))
    {
        $pluginName = $plugin->plugin;
    }
    else // compatible with earlier versions of wordpress
    {
        $pluginName = $plugin;
    }

    // Allow all plugins except the ones listed above to be updated
    if (!in_array(trim($pluginName),$pluginsNotToUpdate))
    {
        // error_log("plugin {$pluginName} is not in list allowing");
        return true; // return true to allow update to go ahead
    }

    // error_log("plugin {$pluginName} is in list trying to abort");
    return false;
}

// Now set that function up to execute when the admin_notices action is called
// Important priority should be higher to ensure our plugin gets the final say on whether the plugin can be updated or not.
// Priority 1 didn\'t work
add_filter( \'auto_update_plugin\', \'filter_plugins_example\' ,20  /* priority  */,2 /* argument count passed to filter function  */);
$plugin对象具有以下内容:

stdClass Object
(
    [id] => 10696
    [slug] => phpbb-single-sign-on
    [plugin] => phpbb-single-sign-on/connect-phpbb.php
    [new_version] => 0.9
    [url] => https://wordpress.org/plugins/phpbb-single-sign-on/
    [package] => https://downloads.wordpress.org/plugin/phpbb-single-sign-on.zip
)

结束

相关推荐

WordPress Themes and PHP unit

有没有将PHP单元测试与WordPress主题结合使用的例子,似乎很多关于这个主题的博客都过时了,因为核心单元测试是trac的一部分,然而昨天晚上,我拉了trac,试图设置一个示例主题来运行测试。它不能安静地工作,你必须运行所有的测试来测试你的主题,即使你尝试并需要一个文件-它试图在WordPress被实例化之前需要它,它会变得一团糟。无论如何,我知道有一个用于主题单元测试的插件,但它只是安装了大量“尝试”并破坏主题的帖子,并没有测试底层逻辑,尤其是在构建框架的情况下,php单元在这里是最好的,因为您可以