This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
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
- $page_rewrite: (string[]) Array of rewrite rules for the “page” post type, keyed by their regex pattern.
Below is the 1 parameter required to use this hook.
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);
Contact Us
If you need any customization or have trouble using this hook, please contact us. We’d be happy to assist you.
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.