Replace page with another page but preserve URL

Question

We have 2 pages with the slugs:

  • /about
  • /about-us

We have a popup on the /about page. Based on the selection the user makes, we want to reload the page and replace the /about page with /about-us but preserve the URL as /about

I have the following solution:

function custom_rewrite_basic() {
    $user_selection = get_user_selection();
 
    if ( 'yes' === $user_selection ) {
        add_rewrite_rule( '^about/?', 'index.php?page_id=12', 'top' ); // 12 is the ID of /about-us page
    }

    flush_rewrite_rules();
}
add_action( 'init', 'custom_rewrite_basic' );

As per the WordPress documentation, flush_rewrite_rules() is an expensive operation and I was wondering if there was a better solution which won’t degrade performance?

0
Siddharth Thevaril 2 months 0 Answers 9 views 0

Leave an answer