Use alternative front page if cookie is set

Question

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.com" 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.com/alt-home" page, it redirects me to "mywebsite.com". I would like the page to be able to be viewed in its custom URL.

0
AncientRo 2 years 2020-10-23T05:10:21-05:00 0 Answers 9 views 0

Leave an answer

Browse
Browse