0

I need to flush rewrite rules every time a term is created or edited.

I tried several ways with no success:

First try

function pec_flush_rewrite () {
    flush_rewrite_rules();
}
add_action( 'edit_term', 'pec_flush_rewrite', 10, 2 );

Second try

function pec_flush_rewrite () {
    flush_rewrite_rules();
}
add_action( 'edited_post_tag', 'pec_flush_rewrite', 10, 2 );

Third try

function pec_flush_rewrite () {
    global $wp_rewrite;
    $wp_rewrite -> flush_rules( true );
}
add_action( 'edit_term', 'pec_flush_rewrite', 10, 2 );

Fourth try

function pec_flush_rewrite () {
    global $wp_rewrite;
    $wp_rewrite -> flush_rules( true );
}
add_action( 'edited_post_tag', 'pec_flush_rewrite', 10, 2 );

Does anyone know where is the problem?

  • Rewrite rules shouldn't need to be flushed whenever a term is created. They should only need to be flushed when new rewrite rules are created, and they are not created for individual terms. Why are you trying to do this? – Jacob Peattie Dec 1 '17 at 9:00
  • Because I'm creating a new rewrite rule every time I create a term. – MKay Dec 1 '17 at 9:09
  • Where do you add the new rules? They need to be added before you try to flush them. – Milo Dec 1 '17 at 14:01

Your Answer

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

Browse other questions tagged or ask your own question.