plugin development - Rewrite url for existing page without flush_rewrite_rules

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

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?

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?

Share Improve this question asked Oct 15, 2019 at 10:28 HowardHoward 1131 gold badge5 silver badges15 bronze badges 3
  • 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
Add a comment  | 

1 Answer 1

Reset to default 7 +50

If you want to rewrite example/test123 (a standard WordPress Page) to example/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/test123 is essentially the same as requesting example/test234 where the HTTP headers and page header, content, footer, etc. would be the same.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745050706a4608369.html

相关推荐

  • plugin development - Rewrite url for existing page without flush_rewrite_rules

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

    22小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信