Sandeep Kumar Mishra
Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.
Ever wanted to filter the list of network-allowed themes in WordPress? Well, the network_allowed_themes filter is here to help! As an Indian developer, I find it super useful to tweak themes across multiple sites in a network effortlessly.
To get started with the network_allowed_themes filter, first, you need to register it using add_filter. You can place this code in the functions.php file of your active theme or a custom WordPress plugin. Personally, I always prefer creating a custom WordPress plugin. This way, nothing breaks when you update your theme in the future.
In the example below, we define a function weplugins_modify_network_allowed_themes_defaults which takes two parameters. Then, we register this function using add_filter. The first parameter is the name of the hook, the second is the name of the function to call, the third is the priority, and the last is the number of arguments.
Sometimes, you may need to remove a registered hook. You can do this using remove_filter to remove the network_allowed_themes filter.
Below are the 2 parameters required to use this hook:
Here’s a basic example of how you can use this hook:
function weplugins_modify_network_allowed_themes_defaults($allowed_themes, $blog_id) {
// Update the $allowed_themes variable according to your requirements
return $allowed_themes;
}
// Add the filter
add_filter("network_allowed_themes", "weplugins_modify_network_allowed_themes_defaults", 10, 2);
In this example, we modify the $allowed_themes variable conditionally:
function weplugins_modify_network_allowed_themes_conditionally($allowed_themes, $blog_id) {
if ($blog_id == 1) {
$allowed_themes[] = 'twentytwentyone';
}
return $allowed_themes;
}
// Add the filter
add_filter("network_allowed_themes", "weplugins_modify_network_allowed_themes_conditionally", 10, 2);
To remove a hook callback, use the example below:
// Remove the filter
remove_filter("network_allowed_themes", "weplugins_modify_network_allowed_themes_defaults", 10, 2);
Make sure to provide the same callback function name, priority, and number of arguments when removing the hook callback.
If you’re having any trouble using this hook or need customizations, please contact us. We’d be happy to assist you!
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.