admin管理员组文章数量:1122846
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.
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.
Share Improve this question edited Jun 27, 2024 at 11:05 Dejan Dozet asked Jun 27, 2024 at 10:41 Dejan DozetDejan Dozet 1135 bronze badges 4 |1 Answer
Reset to default 0I 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.
本文标签: filtersHow to Dynamically Switch WordPress Front Page Without Causing Canonical Redirect
版权声明:本文标题:filters - How to Dynamically Switch WordPress Front Page Without Causing Canonical Redirect? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302491a1931554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
home.php
orfrontpage.php
and the answer would be to use anif ( 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, 2024 at 12:54