17

There is an opt-in filter that allows all plugins on my site to receive automatic updates:

add_filter( 'auto_update_plugin', '__return_true' );

I like this feature, but I don't want all my plugins to be updated automatically. How can I allow some plugins to be updated automatically, while excluding those I want to do manually?

0

2 Answers 2

21

Instead of using the code from the question in functions.php, replace it with this:

/**
 * 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 );

This code can easily be tweaked to customize theme and core updates, too.

Plugin and theme update statistics were added in Wordpress 3.8.2 (27905). The above function uses the slug to identify the plugins, but you can use any of the object's info (in $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

For Wordpress 3.8.1 and below, use this function instead:

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 );

Props go to @WiseOwl9000 for pointing out the change with WP 3.8.2

2
  • @kaiser Nice idea with condensing the code. It's been awhile since I looked at this, but on first glance it looks like this reverses the logic. Did you test this? It appears that the items in the array are now the only ones that would get auto-updated, and everything else would be excluded.
    – David
    Oct 19, 2014 at 7:50
  • David, you were completely right: Fixed and +1ed
    – kaiser
    Oct 19, 2014 at 13:49
3

Note as of wordpress 3.8.2 the type of the plugin item passed to this function has changed and it is now an object.

/**
 * @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  */);

The $plugin object has the following:

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
)
3
  • I like your answer, but it would be great if you can add documentation to support that for further reading. Thanks Apr 10, 2014 at 9:56
  • The only reference I could find in the codex to controlling plugin updates is here: codex.wordpress.org/… I couldn't find anything in any change logs to support the change to an object instead of a string being passed. Apr 11, 2014 at 3:37
  • I edited/updated my answer to account for the change. Here's the changeset you were looking for: core.trac.wordpress.org/changeset/27905
    – David
    Apr 28, 2014 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.