Change custom rewrite rule when permalink is updated/changed

by ash1f   Last Updated May 16, 2019 05:08 AM

I have a scenario where i need to make a rewrite rule dynamic so that when permalink is changed the re-write rule should get updated as well

function rewrite_rule() { 
    global $wp_rewrite;
    $post = get_post($result_id);
    $post_slug  = $post_id  ->post_name


    add_rewrite_rule('^'.$post_slug.'/([^/]*)/?','index.php?post_type=page&page_id=1234&term=$matches[1]','top');

    $wp_rewrite->flush_rules(true); 
} 

add_action('init', 'rewrite_rule' );

In this case

$post_slug

should be the permalink slug value from wordpress and everytime user changes the permalink value the rewrite should be updated with the value as well



Related Questions


Updated June 28, 2017 10:08 AM

Updated June 05, 2016 08:03 AM

Updated February 19, 2017 04:08 AM

Updated July 04, 2018 21:08 PM

Updated April 27, 2019 14:08 PM