如何排除自动更新的插件?


16

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

add_filter( 'auto_update_plugin', '__return_true' );

我喜欢此功能,但我不希望所有插件都自动更新。如何排除某些我想手动执行的插件,而又允许它们自动更新?

Answers:


20

与其在functions.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来标识插件,但是您可以使用任何对象的信息(在$ item中):

[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的更改


@kaiser压缩代码的好主意。自从我看过这已经有一段时间了,但是乍一看,这似乎颠倒了逻辑。你测试了吗?看来,数组中的项目现在是唯一可以自动更新的项目,其他所有项目都将被排除。
大卫

大卫,您是完全正确的:固定和+1操作
kaiser

3

请注意,自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
)

我喜欢您的回答,但是如果您可以添加文档以支持进一步阅读,那就太好了。谢谢
Pieter Goosen 2014年

我在法典中可以找到的唯一有关控制插件更新的参考是:codex.wordpress.org/… 我在任何变更日志中都找不到任何东西来支持对对象的变更,而不是传递字符串。
WiseOwl9000 2014年

我修改/更新了我的答案以说明更改。这是您正在寻找的变更集:core.trac.wordpress.org/changeset/27905
David
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.