I have created pages with slug test123 and test234 in wordpress, I want to create a plugin that doing the following

  • when user request test123, and the content in test234 will be served instead.

I understand I can use the rewrite API but it is possible to do so without the need of flush_rewrite_rules? That is, without setting the rules in database?

  • If you just want the test123 page to display the post content of the test234 page, you could simply use a shortcode or edit the page template to query and display the test234 content? Why do you need the URL rewrite?
    – Sally CJ
    Commented Oct 16, 2019 at 15:02
  • @SallyCJ because I have a lot of pages need to be rewrited at once. I would like to keep the mapping in a PHP file (that is my plugin) instead of flushing rewrite rules in db every-time I need to update.
    – Howard
    Commented Oct 28, 2019 at 6:53
  • In that case, you can try the request or parse_request hook..
    – Sally CJ
    Commented Oct 28, 2019 at 8:00

1 Answer 1


If you want to rewrite example.com/test123 (a standard WordPress Page) to example.com/test234 (another standard WordPress Page) without having to save the rewrite rules in the database, then one option is using the request filter hook:

add_filter( 'request', function ( $query_vars ) {
    if ( isset( $query_vars['pagename'] ) ) {
        $slug = $query_vars['pagename'];

        // Define a list of source and target Page slugs.
        $mapping = [
            'test123' => 'test234',
            'test567' => 'foo-bar',

        // If the requested slug is in the mapping list, change the requested page slug.
        if ( ! empty( $mapping[ $slug ] ) ) {
            $query_vars['pagename'] = $mapping[ $slug ];
    return $query_vars;
} );

You can also use the parse_request action hook, but the above should be fine, so I'm not including the code for that action hook.

But whether you use the filter hook or the action hook, the trick is to internally change the requested page slug if it matches the source slug in the mapping list/array. That way, requesting (or visiting) example.com/test123 is essentially the same as requesting example.com/test234 where the HTTP headers and page header, content, footer, etc. would be the same.

  • Thanks, actually I am wondering if this work, why a lot of plugin developers still need to save rules on db..
    – Howard
    Commented Oct 30, 2019 at 3:57
  • 1
    Well, these are just my personal thoughts. 1) It's a standard practice where one would normally use add_rewrite_rule() which by default saves the rules in the database (the other option is, written to the htaccess file). 2) There would not be any necessity for the extra step where we alter the requested page slug after it was actually determined - WP does that by looping through the saved rewrite rules in the database.
    – Sally CJ
    Commented Oct 30, 2019 at 7:26
  • 1
    (cont) 3) Maybe their rules are not constantly changing like yours, so they only need to flush the rules upon activation/deactivation of their plugin. Which means, you may programmatically flush the rules so long as it's done properly.. 4) They don't know about the trick mentioned in the answer.. or they just prefer writing the rules to the database..
    – Sally CJ
    Commented Oct 30, 2019 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.