How to Enable Automatic Updates for Specific Plugins in WordPress
WordPress provides a filter and a user interface to indicate which plugins should be updated automatically.
Problem
In another tutorial I wrote that automatic updates are the easiest method to update plugins in WordPress. Add the following code and WordPress will update each plugin once a new version is available. It uses the auto_update_plugin
filter.
// Enable automatic updates for all plugins
add_filter('auto_update_plugin', '__return_true');
Despite being the easiest solution, I do not like it because a plugin updated automatically can break the entire website if it is not compatible with the current theme or other plugins.
Exception
Sometimes we use plugins that add minor features to the website and work completely isolated from other plugins and the current theme. I call them “helper plugins”. A plugin to replace attachments is a good example.
In these cases, enabling automatic updates may be a good choice. Add the following code and WordPress will enable automatic updates for plugins whose slug is listed in the $allowed
array.
/*
* Enables automatic updates for specific plugins.
*/
function ns_auto_update_specific_plugins($update, $plugin){
$allowed = array('plugin-1', 'plugin-2', 'plugin-3');
return in_array($plugin->slug, $allowed) ? true : $update;
}
add_filter('auto_update_plugin', 'ns_auto_update_specific_plugins', 10, 2);
User interface
WordPress 5.5 added a user interface to make the procedure described above easier. Navigate to Plugins > Installed Plugins and note the Automatic Updates column. For each plugin, this column displays a link to enable or disable automatic updates.
Further reading
I recommend the other tutorials in this series to learn more about managing plugins in WordPress.
Source code
The source code developed in this tutorial is available here.
Comments