Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use network_allowed_themes filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
August 2, 2022
5 minutes read

network_allowed_themes filter

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.

Parameters

    Below are the 2 parameters required to use this hook:

  • $allowed_themes: (string[]) An array of theme stylesheet names.
  • $blog_id: (int) ID of the site.

Live Examples

Example 1: Basic Usage

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);
    

Example 2: Conditional Modification

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);
    

Example 3: Removing the Hook

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.

Access Premium WordPress Plugins

Contact Us

If you’re having any trouble using this hook or need customizations, please contact us. We’d be happy to assist you!

Sandeep Kumar Mishra

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.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.