最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - Best way to trigger rewrite rules

programmeradmin1浏览0评论

I typically call the rewrite rules with this action

add_action('init', 'add_my_rewrite_rules')

But what I do not like about it is that the rewrite rules are written every time an init action is triggered, which means pretty much every time a page is refreshed.

Is this the correct action to call the rewrite rules? Is there a better way of handling that? (i.e, no need to add the rules every time we trigger init)

Thanks.

I typically call the rewrite rules with this action

add_action('init', 'add_my_rewrite_rules')

But what I do not like about it is that the rewrite rules are written every time an init action is triggered, which means pretty much every time a page is refreshed.

Is this the correct action to call the rewrite rules? Is there a better way of handling that? (i.e, no need to add the rules every time we trigger init)

Thanks.

Share Improve this question asked Feb 26, 2021 at 0:00 GreesoGreeso 2,2247 gold badges33 silver badges57 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

init is the recommended hook and no, the rewrite rules will not be overwritten each time the hook fires or that the page is loaded, unless if you call flush_rewrite_rules() or WP_Rewrite::flush_rules() in your callback.

add_action( 'init', 'add_my_rewrite_rules' );
function add_my_rewrite_rules() {
    add_rewrite_rule( ... );
    flush_rewrite_rules(); // don't do this
}

So please don't do that, or do it only upon plugin/theme activation and deactivation — and if you don't already know, you can easily flush the rules by simply going to the Permalink settings admin page, without having to hit the Save Changes button.

发布评论

评论列表(0)

  1. 暂无评论