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

How to use customize_allowed_urls filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
March 18, 2023
5 minutes read
So, you’re diving into the world of WordPress hooks, huh? Well, you’ve come to the right place! Let’s chat about the customize_allowed_urls filter. This nifty little filter helps you manage which URLs can be clicked and followed while you’re tinkering around in the Customizer preview. Pretty cool, right?

To get started with the customize_allowed_urls filter, you need to register it using add_filter. You can pop this code into the functions.php file of your active theme or, better yet, create a custom WordPress plugin. Personally, I always recommend the plugin route—keeps things neat and tidy for when you update your theme.

Example 1: Basic Usage of customize_allowed_urls

Let’s kick things off with a straightforward example. Here, we’ve defined a function called weplugins_modify_customize_allowed_urls_defaults to manage our allowed URLs.

        function weplugins_modify_customize_allowed_urls_defaults($allowed_urls) {
            // Update the $allowed_urls variable according to your website requirements and return this variable.
            // You can modify the $allowed_urls variable conditionally too if you want.
            return $allowed_urls;
        }
        // add the filter
        add_filter("customize_allowed_urls", "weplugins_modify_customize_allowed_urls_defaults", 10, 1);
        

Example 2: Removing the Hook Callback

Need to remove a hook callback? No worries! Just use the remove_filter function like so:

        remove_filter("customize_allowed_urls", "weplugins_modify_customize_allowed_urls_defaults", 10, 1);
        

Make sure to provide the same callback function name, priority, and number of arguments when removing the hook callback.

Example 3: Advanced Customization

For those looking to dive deeper, let’s explore an advanced customization scenario. This time, we’re going to conditionally modify the URLs based on specific requirements.

        function weplugins_advanced_customize_allowed_urls($allowed_urls) {
            // Let's say you want to allow URLs only on certain conditions.
            if (some_condition) {
                $allowed_urls[] = 'https://example.com';
            }
            return $allowed_urls;
        }
        add_filter("customize_allowed_urls", "weplugins_advanced_customize_allowed_urls", 10, 1);
        

Below is an example of how you can use this hook:

apply_filters('customize_allowed_urls', string[] $allowed_urls);

Contact Us

If you need any customization or assistance with this hook, feel free to contact us at WePlugins. We’re here to help!

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.