Automatically Update Your WordPress Plugins and Themes and Translations

WordPress allows you to automatically update your WordPress plugins and themes and translations by adding some PHP constants and filters to your WordPress installation.

Constants are best off added in /wp-config.php and filters are preferred to be used in a wp-content/mu-plugins/ plugin, to create a mu-plugin is easy for example just create a updates.php file with the content below and file it in /wp-content/mu-plugins directory if mu-plugins folder doesn’t exist just create and name the folder.

<?php

/*
 Plugin Name: UpdateWordPress
 Plugin URI: https://wpbeaches.com
 Description: This updates stuff
 Author:WP Beaches
 Author URI: https://wpbeaches.com
 License: GPL2
 */

add_filter( 'auto_update_plugin', '__return_true' ); // Updates Plugins
add_filter( 'auto_update_theme', '__return_true' ); // Updates Themes
add_filter( 'auto_update_translation', '__return_true' ); // Updates Translations

So the above will update all plugins, themes and translations, to reverse the behaviour and not update you can either change the return value to __return_false or simply not add the filter.

When does it actually update? it won’t do it straight away,  give it a bit of time and soon the automagic updates will kick in.

Updates to WordPress Itself

By default any minor updates or what is known as point releases are by default enabled – here are some constants you can add in wp-config.php to change that behaviour.

define( 'AUTOMATIC_UPDATER_DISABLED', true );

Disable all the things, WordPress, themes, plugins and translations

define( 'WP_AUTO_UPDATE_CORE', true );

Enable major WordPress core releases, like going from a 4.7 to a 4.8

define( 'WP_AUTO_UPDATE_CORE', false );

Disable all WordPress releases major and minor

define( 'WP_AUTO_UPDATE_CORE', minor );

Enable WordPress minor updates, disable WordPress major updates – this is the default.

 

Some more filters via a mu-plugin

So instead of the constant declaration you can also do these update preferences via filters in the mu-plugin

<?php

/*
 Plugin Name: UpdateWordPress
 Plugin URI: https://wpbeaches.com
 Description: This updates stuff
 Author: WP Beaches
 Author URI: https://wpbeaches.com
 License: GPL2
 */

add_filter( 'automatic_updater_disabled', '__return_true' ); // Disable all updates WP, themes, plugins, translations

add_filter( 'auto_update_core', '__return_true' ); Enable all WordPress updates
 
add_filter( 'allow_dev_auto_core_updates', '__return_true' ); // Enable WordPress development updates
add_filter( 'allow_minor_auto_core_updates', '__return_true' ); // Enable WordPress minor updates
add_filter( 'allow_major_auto_core_updates', '__return_true' ); // Enable WordPress major updates

For the last 4 to disable the WordPress updates you can change the value to __return_false

 

Don’t Update Specific WordPress Plugins

You can manipulate the filter auto_update_plugins ,  lets say you where happy updating all plugins bar a few suspect ones you could do this…

<?php
/*
Plugin Name: Don't Update Some Plugins
Plugin URI: https://wpbeaches.com
Description: This doesn't update some plugins
Author: Neil Gowran
Author URI: https://wpbeaches.com
License: GPL2
*/
add_filter( 'auto_update_plugin', 'do_not_auto_update_specific_plugins', 10, 2 );
// Don't update some plugins
function do_not_auto_update_specific_plugins ( $update, $item ) {
// Array of plugin slugs to never auto-update
$plugins = array (
'akismet',
'duplicator',
);
if ( in_array( $item->slug, $plugins ) ) {
return false; // Never update plugins in this array
} else {
return true; // Else update the rest
}
}
view raw dont-update.php hosted with ❤ by GitHub

So here I am in the above code snippet updating all plugins apart from Akismet and Duplicator – these are just examples, reality is I would have no issues auto updating these ones, we all know which ones give us grief, and for the ones that do cause grief on update just add them to the comma separated array using their lowercase slug name as named in the plugin repo.

 

Ref

2 Comments

  1. Damien on February 27, 2017 at 10:42 am

    Great to know – I’ve added the code to a MultiSite installation that I use during development. It has so many plugins that I’m updating them daily. This will help.

    Aside: Tiny mistake in second code snippet – the ‘Enable all WordPress updates’ comment is missing the ‘//’ comment opener.

    • Neil Gowran on February 27, 2017 at 9:32 pm

      Thanks for spotting – updated

Leave a Comment





%d bloggers like this: