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

How to use display_site_states filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
June 24, 2023
5 minutes read

Ah, WordPress hooks! They are like the secret spices that can transform your WordPress site into something truly unique. As a fellow developer from India, I can say that using hooks is like adding a pinch of masala to your code—it just makes everything better. One such interesting hook is the display_site_states filter. This filter lets you modify the default site states in the Sites list table. Let’s dive into how you can use it effectively.

Example 1: Modifying Default Site States

Here’s a simple example to modify the default site states using the display_site_states filter. You can write this code into the functions.php of your activated theme or in a custom WordPress Plugin.

    function weplugins_modify_display_site_states_defaults($site_states, $site) { 
        // Update the $site_states variable according to your website requirements.
        return $site_states; 
    }
    // Add the filter
    add_filter("display_site_states", "weplugins_modify_display_site_states_defaults", 10, 2);
    

Example 2: Removing a Hook Callback

Sometimes you might need to remove a registered hook. Here’s how you can use remove_filter to remove the display_site_states filter.

    remove_filter("display_site_states", "weplugins_modify_display_site_states_defaults", 10, 2);
    

Ensure you provide the same callback function name, priority, and number of arguments while removing the hook.

Example 3: Conditional Modifications

You might want to modify the site states based on certain conditions. Here’s how you can do it:

    function weplugins_conditional_display_site_states($site_states, $site) { 
        if ($site->id == 1) { // Example condition
            // Modify $site_states for the main site
        }
        return $site_states; 
    }
    add_filter("display_site_states", "weplugins_conditional_display_site_states", 10, 2);
    

Now, if you’re having any trouble using this hook, or if you’re looking to get some customization done, feel free to Contact Us. We at WePlugins are always ready to assist you with your WordPress needs.

Access Premium WordPress Plugins

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.