最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - Use alternative front page if cookie is set

programmeradmin2浏览0评论

By default, I present the user with the "Home" front page (set in "Settings / Reading / Homepage").

I have a duplicate page of this called "Alt Home", with some changed content.

When the user goes to the front page, if the "use_alt_home" cookie exists (which I set based on certain conditions), I want to show the user the "Alt Home" page instead (not redirect, load the "Alt Home" page at the "mywebsite" address, instead of the "Home" page).

I tried the following code:

function use_alt_home_page( $page_id ) {
    $alt_home_id = 105;

    if ( is_admin() ) {
        return $page_id;
    }

    if ( isset( $_COOKIE['alt_home_page'] ) ) {
        return $alt_home_id;
    }

    return $page_id;
}

add_filter( 'option_page_on_front', 'use_alt_home_page', 20 );

This works on my localhost but when I deploy I get the "ERR_TOO_MANY_REDIRECTS" error.

Is there another way I can code the alternative home page, or using this same method, fix the redirects error?

One other issue is that when I want to go directly to the "mywebsite/alt-home" page, it redirects me to "mywebsite". I would like the page to be able to be viewed in its custom URL.

By default, I present the user with the "Home" front page (set in "Settings / Reading / Homepage").

I have a duplicate page of this called "Alt Home", with some changed content.

When the user goes to the front page, if the "use_alt_home" cookie exists (which I set based on certain conditions), I want to show the user the "Alt Home" page instead (not redirect, load the "Alt Home" page at the "mywebsite" address, instead of the "Home" page).

I tried the following code:

function use_alt_home_page( $page_id ) {
    $alt_home_id = 105;

    if ( is_admin() ) {
        return $page_id;
    }

    if ( isset( $_COOKIE['alt_home_page'] ) ) {
        return $alt_home_id;
    }

    return $page_id;
}

add_filter( 'option_page_on_front', 'use_alt_home_page', 20 );

This works on my localhost but when I deploy I get the "ERR_TOO_MANY_REDIRECTS" error.

Is there another way I can code the alternative home page, or using this same method, fix the redirects error?

One other issue is that when I want to go directly to the "mywebsite/alt-home" page, it redirects me to "mywebsite". I would like the page to be able to be viewed in its custom URL.

Share Improve this question asked Oct 23, 2020 at 9:50 AncientRoAncientRo 3663 silver badges12 bronze badges 2
  • If it's just the post content that's different, you could just use the the_content filter? Or in your template, use a conditional to display the alternative home page's content.. – Sally CJ Commented Oct 23, 2020 at 16:15
  • That is what I ended up doing. The page uses Advanced Custom Fields and it has tens of "get_field()" functions that retrieve the page data. If the cookie is set, I give the "get_field()" functions the ID of the alternative page, otherwise I let it use the default page ID. – AncientRo Commented Oct 26, 2020 at 7:51
Add a comment  | 

1 Answer 1

Reset to default 0

I ended up using a workaround method: In the home page template file, I verify if the cookie is set and if so, I retrieve the data from the alternative page, as opposed to from the current page.

发布评论

评论列表(0)

  1. 暂无评论