Adding custom URL rewrite rules in WordPress using add_rewrite_rule

The add_rewrite_rule function in WordPress allows developers to create custom URL rewrite rules for their WordPress site. This can be useful for creating custom permalink structures, redirecting URLs, or creating custom endpoints for specific content types.

By using the add_rewrite_rule function, developers can have more control over the URLs on their WordPress site and create more user-friendly and SEO-friendly URLs for their content.

Parameters Accepted by the WordPress add_rewrite_rule Function

The add_rewrite_rule function accepts the following parameters:

  • $regex (string, required): Regular expression to match request against.
  • $query (string array, required): The corresponding query vars for this rewrite rule.
  • $after (string, optional, default value: ‘bottom’): Priority of the new rule. Accepts ‘top’ or ‘bottom’. Default is ‘bottom’.

When called, the add_rewrite_rule function does not return a value.

Examples

How to add a simple rewrite rule in WordPress

add_action('init', 'custom_rewrite_rule');
function custom_rewrite_rule() {
 add_rewrite_rule('^my-page/?', 'index.php?pagename=my-page', 'top');
}

This code snippet adds a simple rewrite rule to WordPress using the add_rewrite_rule function. It hooks into the init action and defines a custom rewrite rule for the URL my-page, which will be mapped to the my-page page in WordPress.

How to add a rewrite rule with custom query variables in WordPress

add_action('init', 'custom_rewrite_rule');
function custom_rewrite_rule() {
 add_rewrite_rule('^product/([^/]+)/?$', 'index.php?product=$matches[1]', 'top');
 add_rewrite_tag('%product%', '([^/]+)');
}

This code snippet adds a rewrite rule with custom query variables to WordPress. It hooks into the init action and defines a custom rewrite rule for the URL pattern product/([^/]+)/?, which will be mapped to a query variable product in WordPress. The add_rewrite_tag function is used to define the custom query variable %product%.

How to add a rewrite rule with custom endpoints in WordPress

add_action('init', 'custom_rewrite_rule');
function custom_rewrite_rule() {
 add_rewrite_rule('^api/(.+?)/?$', 'index.php?custom_endpoint=$matches[1]', 'top');
 add_rewrite_endpoint('api', EP_PERMALINK);
}

This code snippet adds a rewrite rule with custom endpoints to WordPress. It hooks into the init action and defines a custom rewrite rule for the URL pattern api/(.+?)/?, which will be mapped to a custom endpoint api in WordPress. The add_rewrite_endpoint function is used to define the custom endpoint api.

Conclusion

In conclusion, the add_rewrite_rule function is an useful component for customizing the URL structure of a WordPress site. By using this function, developers can create custom URL patterns and redirect rules to improve the user experience and make the site more search engine friendly. With careful planning and implementation, add_rewrite_rule can be a valuable asset in creating a more flexible and user-friendly website.

Related WordPress Functions