How to Enable Automatic Updates for a Theme in WordPress
WordPress provides a filter and a user interface to indicate which themes should be updated automatically.
Problem
In another tutorial I wrote that automatic updates are the easiest method to update themes in WordPress. Add the following code and WordPress will update each theme once a new version is available. It uses the auto_update_theme
filter.
// Enable automatic updates for all themes
add_filter('auto_update_theme', '__return_true');
The main risk of this method is that problems may happen if the active theme is updated automatically, but the new version is incompatible with plugins, custom CSS rules, etc.
Exception
If you trust enough in your theme provider and conflicts are unlikely to occur after an update, a good decision would be to turn on automatic updates for the active theme and other installed themes of your choice.
Add the following code and WordPress will enable automatic updates for themes whose slug is listed in the $allowed
array.
/*
* Enables automatic updates for specific themes.
*/
function ns_auto_update_specific_themes($update, $theme){
$allowed = array('theme-1', 'theme-2', 'theme-3');
return in_array($theme->slug, $allowed) ? true : $update;
}
add_filter('auto_update_theme', 'ns_auto_update_specific_themes', 10, 2);
User interface
WordPress 5.5 added a user interface to make the procedure described above easier.
Step 1: Navigate to Appearance and click on a theme.
Step 2: Click the link to enable or disable automatic updates.
Tip
A WordPress website only requires one theme, the active theme. Delete other installed themes whenever possible. These themes are a waste of resources, and their vulnerabilities are open doors for attackers.
Further reading
I recommend the other tutorials in this series to learn more about managing themes in WordPress.
Source code
The source code developed in this tutorial is available here.
Comments