admin管理员组

文章数量:1023867

How can I flush rewrite rules after saving or updating any page? Pretty much a newbie and trying to figure out how this would work.

add_action( 'save_post', 'my_save_post_function', 10, 3 );
function my_save_post_function( ) {
flush_rewrite_rules();
}

How can I flush rewrite rules after saving or updating any page? Pretty much a newbie and trying to figure out how this would work.

add_action( 'save_post', 'my_save_post_function', 10, 3 );
function my_save_post_function( ) {
flush_rewrite_rules();
}
Share Improve this question edited Apr 20, 2019 at 22:20 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Apr 20, 2019 at 21:52 AlanAlan 111 bronze badge 1
  • 2 Do not flush rules here. Have a read this "usage" note. – nmr Commented Apr 20, 2019 at 22:52
Add a comment  | 

1 Answer 1

Reset to default 0

I haven't worked much with flush_rewrite_rules function, but based on the usage notes mention by nmr I think you could perhaps do something like this,

add_action( 'save_post', 'my_save_post_function' );
function my_save_post_function( $post_id ) {
  $maybe_some_extra_logic = true; // if applicable
  if ( $maybe_some_extra_logic && ! has_action( 'shutdown', 'my_flush_rewrite_rules' ) ) {
    add_action( 'shutdown', 'my_flush_rewrite_rules', 9999 ); // set suitable priority
  }
}

function my_flush_rewrite_rules() {
  flush_rewrite_rules();
}

How can I flush rewrite rules after saving or updating any page? Pretty much a newbie and trying to figure out how this would work.

add_action( 'save_post', 'my_save_post_function', 10, 3 );
function my_save_post_function( ) {
flush_rewrite_rules();
}

How can I flush rewrite rules after saving or updating any page? Pretty much a newbie and trying to figure out how this would work.

add_action( 'save_post', 'my_save_post_function', 10, 3 );
function my_save_post_function( ) {
flush_rewrite_rules();
}
Share Improve this question edited Apr 20, 2019 at 22:20 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Apr 20, 2019 at 21:52 AlanAlan 111 bronze badge 1
  • 2 Do not flush rules here. Have a read this "usage" note. – nmr Commented Apr 20, 2019 at 22:52
Add a comment  | 

1 Answer 1

Reset to default 0

I haven't worked much with flush_rewrite_rules function, but based on the usage notes mention by nmr I think you could perhaps do something like this,

add_action( 'save_post', 'my_save_post_function' );
function my_save_post_function( $post_id ) {
  $maybe_some_extra_logic = true; // if applicable
  if ( $maybe_some_extra_logic && ! has_action( 'shutdown', 'my_flush_rewrite_rules' ) ) {
    add_action( 'shutdown', 'my_flush_rewrite_rules', 9999 ); // set suitable priority
  }
}

function my_flush_rewrite_rules() {
  flush_rewrite_rules();
}

本文标签: How do I flush rewrite rules