My Plugin

I implemented a small plugin to be able to show events with a specific custom field in a special order. To have some nice permalinks I also used the add_rewrite_rule function to add those rules. Here is the important part of my plugin:

// Add rewrite rule for event archives on activation
function event_archive_activate() {
    add_rewrite_rule( '^(januar|februar|maerz|april|mai|juni|juli|august|september|oktober|november|dezember)([0-9]+)', 'index.php?pagename=event_archive&event_month=$matches[1]&event_year=$matches[2]', 'top' );
register_activation_hook( __FILE__, 'event_archive_activate' );

// Remove rewrite rule for event archives
function event_archive_deactivate() {
register_deactivation_hook( __FILE__, 'event_archive_deactivate' );

// Register the custom query var so WP recognizes it
function event_archive_add_query_vars( $vars ) {
    $vars[] = 'event_month';
    $vars[] = 'event_year';
    return $vars;
add_filter( 'query_vars', 'event_archive_add_query_vars' );

The problem

So I upload this plugin, actitave it and the rule is successfully written to the options table. Everything is working just fine and all the permalinks shows the correct pages.

But after a while, the rewrite rule disappears from the rewrite rules in the options table. I searched for another call of the flush_rewrite_rules() function my WordPress directory, but only ma plugin and the core files are using it.

I have no idea where and when the rule is deleted from the options table and how to prevent it in the future. My only solution so far is to deactivate the plugin and reactivate it, to have the rules saved to the options table again.


// Flush added rewrite rules on activation
function event_archive_activate() {
register_activation_hook( __FILE__, 'event_archive_activate' );

// Add rewrite rule for event archive on init
function event_archive_set_rewrite_rules() {
    add_rewrite_rule( '^(januar|februar|maerz|april|mai|juni|juli|august|september|oktober|november|dezember)([0-9]+)', 'index.php?pagename=event_archive&event_month=$matches[1]&event_year=$matches[2]', 'top' );
add_filter( 'init', 'event_archive_set_rewrite_rules' );

Add your rules on the init action and just flush on activation, when some other code flushes them they will still be present.

  • According to the codex, you should not flush it in the init_action as it is quite slow and flushing it on every page load is totall wrong. I am just wondering how some process might remove my rules. I am even not sure how they still can be present, if some other plugin would call the flush function. Am I doing something completely wrong in my plugin? I think not. – 2ndkauboy May 27 '13 at 15:43
  • 1
    @Kau-Boy I didn't say to flush on init, flush on activation as you are doing, but add the rules on init. If you have multiple plugins / theme adding rewrite rules, logically the way you are doing it can never work, as you've already experienced. – Milo May 27 '13 at 16:04
  • also, note that if you just visit the permalinks settings page, core flushes rewrites, so that may be when your rules are disappearing. – Milo May 27 '13 at 16:27
  • If I understand you correct, the code posted in my edited querstion would solve it? But then, what is the flush_rewrite_rules about? If it is always overwritten, when some core funtion calls it and I don't really need it, why is there such a function? So why sould I ever call the event_archive_activate() function if I always have to call the event_archive_set_rewrite_rules() on every page load? – 2ndkauboy May 27 '13 at 17:07
  • it's not always overwritten, only when rules are flushed. after your rules are added and you flush them, for most subsequent requests adding them does nothing, as nothing has flushed them, they're still there. technically, you could check the existing rules and only add them again if they're gone, but that's just pointless overhead, adding them regardless on every request is much easier. – Milo May 27 '13 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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