Control WordPress Plugin and Theme Auto-Updates with auto_update_plugin Filter (WP 5.5)

Control plugin and theme auto-updates with auto_update_plugin

WordPress 5.5 added “Enable auto-updates” links to the Plugins and Themes screens: an administrator can switch automatic updates on per plugin or theme, and WordPress installs new versions in the background twice a day. The filters behind it, auto_update_plugin and auto_update_theme, have existed since WordPress 3.7, and they still decide in the end. With them a developer can set a fixed policy in code that applies on every site.

Problem: On client sites, small utility plugins should update automatically, but WooCommerce, payment gateways and the custom theme must be updated only after testing, whatever somebody clicks in the admin.

Solution: Return true or false from the auto_update_plugin and auto_update_theme filters for the slugs you want to control, and the received value for all others. Put the code into an mu-plugin, so it cannot be deactivated.

Plugins

<?php
add_filter( 'auto_update_plugin', 'ha_plugin_auto_updates', 10, 2 );

function ha_plugin_auto_updates( $update, $item ) {
    // $item->slug is the folder name of the plugin
    $always = array( 'classic-editor', 'redirection', 'wordpress-seo' );
    $never  = array( 'woocommerce', 'woocommerce-gateway-stripe' );

    if ( in_array( $item->slug, $always, true ) ) {
        return true;
    }

    if ( in_array( $item->slug, $never, true ) ) {
        return false;
    }

    return $update; // the choice made in the admin
}

When the filter returns true or false for a plugin, the admin screen shows “Auto-updates enabled/disabled” without a link, so nobody can change it there. Returning $update (which can be null) leaves the choice to the admin.

Themes

<?php
add_filter( 'auto_update_theme', function ( $update, $item ) {
    // the custom theme and its child: always by hand
    if ( in_array( $item->theme, array( 'ha-theme', 'ha-theme-child' ), true ) ) {
        return false;
    }
    return $update;
}, 10, 2 );

Themes and plugins that are not in the official directory get updates only if they have their own update mechanism, so a custom theme normally is not affected anyway; the filter makes the rule explicit.

Core

<?php
add_filter( 'allow_minor_auto_core_updates', '__return_true' );  // 5.5.1 -> 5.5.2: security and fixes
add_filter( 'allow_major_auto_core_updates', '__return_false' ); // 5.5 -> 5.6 only by hand

Minor core updates are automatic by default and should stay so: they are the security releases. To switch off all automatic updates (for example on a site that is deployed from Git), define AUTOMATIC_UPDATER_DISABLED as true in wp-config.php, and make sure your deployment process installs updates instead.

Where to put the code

Filters cannot go into wp-config.php: it runs before the plugin API is loaded. A file in wp-content/mu-plugins/ is loaded on every request, before normal plugins, and also covers all sites of a Multisite network.

NOTE: Automatic updates run without anybody watching. Turn them on only where you have a recent backup and monitoring that tells you when the site breaks; since 5.5, WordPress emails the admin after every automatic update, and those emails are worth reading.