0

Question:

I'm trying to dynamically switch the front page of my WordPress site based on certain conditions (e.g., whether a user is logged in or not). I have two specific pages that I want to set as the front page dynamically.

Initially, I tried using the pre_option_page_on_front filter, but it causes issues with canonical URLs leading to redirects.

Here’s the code I used:

function dynamic_front_page($default) {
    $page_id_1 = 2; // Logged-in users' front page
    $page_id_2 = 10; // Not logged-in users' front page
    
    if (is_user_logged_in()) {
        return $page_id_1;
    } else {
        return $page_id_2;
    }
    return $default;
}

add_filter('pre_option_page_on_front', 'dynamic_front_page');

While this sets the front page correctly, it causes a canonical redirect issue.

How can I dynamically switch the front page without causing canonical redirect issues? Ideally, I’d like to achieve this using hooks and filters, without updating options in the database.

4
  • 1
    instead of changing the page, could you have 1 single page that displays different things to different people? It sounds like you've devised a solution and asked how to fix it, rather than looking for a solution to your problem of showing different things to different people
    – Tom J Nowell
    Commented Jun 27 at 11:01
  • I want to change the front page dynamically and that's it. I hope my explanation related to users will not come to the fore, it was just an example. Commented Jun 27 at 11:05
  • usually someone would ask this about home.php or frontpage.php and the answer would be to use an if ( is_user_logged_in() ) { get_template_part( .. logged in template ... ); } else { get_template_part( ...logged out template... ); }, but it sounds like you've chosen to use a page builder instead. That doesn't mean what you need can't be done, but this particular method of filtering the option that you've chosen won't be easy to fix. Just because you want the contents of that page to show on the frontpage, doesn't mean that page has to be the frontpage
    – Tom J Nowell
    Commented Jun 27 at 12:54
  • No, it is not related to the template files, it is related to dynamically filtering the page_on_front option. I don't need to change the template, I need to change the front page in the code behind. Commented Jun 27 at 13:13

1 Answer 1

0

I found a solution that worked (though I need to test it thoroughly)

First, add a filter on the "page on front" option:

function filter_page_on_front($default) {
  $current_post_language = detect_post_language();
  if ($current_post_language) {
    $intro_page = get_current_page();
    if ($intro_page) {
      $page_on_front = get_option('page_on_front_' . $current_post_language, '');
      if ($page_on_front && $intro_page->ID == $page_on_front) {
        return $page_on_front;
      }
    }
  }
  return $default;
}

add_filter('pre_option_page_on_front', 'filter_page_on_front');

Next is to disable redirects on the front pages:

function wpdocs_disable_frontpage_canonical_redirect($redirect) {
  
  if (is_front_page()) {
    $redirect = false;
  }

  return $redirect;
}

add_filter('redirect_canonical', 'wpdocs_disable_frontpage_canonical_redirect');

and the last is to return the page slug for that other(s) front page:

function custom_page_link($link, $post_id, $sample) {

  $current_post_language = detect_post_language();
  $main_language = get_option('selected_language', 'en_US');
  if ($current_post_language && $current_post_language != $main_language) {
    $page_on_front = get_db_option('page_on_front_' . $current_post_language, '');
    if ($page_on_front && $post_id == $page_on_front) {
      $page_slug = get_post_field('post_name', $page_on_front); // Get the slug of the page
      if ($page_slug) {
        return home_url('/' . $page_slug . '/');
      }
    }
  }

  return $link;
}

add_filter('page_link', 'custom_page_link', 10, 3);

Please, note that get_db_option will get unfiltered options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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