add_rewrite_rule

Home » Functions » add_rewrite_rule

The add_rewrite_rule function in WordPress is used to add a new rewrite rule to the list of existing rewrite rules. Rewrite rules are used to handle URL requests and transform them into parameters that WordPress can understand. This function allows you to customize the URL structure of your WordPress site, giving you more control over how your content is accessed.

Example usage code:

add_action('init', 'custom_rewrite_rules');

function custom_rewrite_rules() {
    add_rewrite_rule('^my-custom-page/([^/]+)/?', 'index.php?pagename=my-custom-page&custom_var=$matches[1]', 'top');
}

In this example, we are adding a custom rewrite rule that will match any URL with the structure "my-custom-page/some-value". The matched value will be stored in the query parameter "custom_var" to be used in the template or plugin code. This allows for dynamic and user-friendly URLs on your WordPress site.

Learn More on WordPress.org

WordPress snippets using the add_rewrite_rule function

Register an account to save your snippets or go Pro to get more features.