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

How to use page_rewrite_rules filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
April 1, 2023
5 minutes read

page_rewrite_rules filter

Filters rewrite rules used for “page” post type archives.

To use page_rewrite_rules filter, first you have to register it using add_filter. You can write this code into the functions.php of your activated theme or in a custom WordPress Plugin. Creating a custom WordPress Plugin while using hooks ensures nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function modify_page_rewrite_rules_defaults which takes one parameter and we registered it using add_filter. The first parameter page_rewrite_rules is the name of the hook, the second parameter modify_page_rewrite_rules_defaults is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook so you can use remove_filter to remove page_rewrite_rules filter.

Parameters

    Below is the 1 parameter required to use this hook.

  • $page_rewrite: (string[]) Array of rewrite rules for the “page” post type, keyed by their regex pattern.

Live Example 1

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

    function weplugins_modify_page_rewrite_rules_defaults($page_rewrite) { 
        // Update the $page_rewrite variable according to your website requirements and return this variable.
        return $page_rewrite; 
    }
    // add the filter
    add_filter("page_rewrite_rules", "weplugins_modify_page_rewrite_rules_defaults", 10, 1);
    

Live Example 2

To remove a hook callback, use the example below.

    remove_filter("page_rewrite_rules", "weplugins_modify_page_rewrite_rules_defaults", 10, 1);
    

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Live Example 3

Here’s another example demonstrating conditional modifications to the rewrite rules.

    function weplugins_modify_page_rewrite_rules_conditionally($page_rewrite) {
        if (is_admin()) {
            // Perform modifications specific to the admin area
        }
        return $page_rewrite;
    }
    add_filter("page_rewrite_rules", "weplugins_modify_page_rewrite_rules_conditionally", 10, 1);
    

Access Premium WordPress Plugins

Contact Us

If you need any customization or have trouble using this hook, 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.