• Scenario: I have a clothing review affiliate website. I have 3 “home” pages: Mens, Womens, and “Both”.

    What I want to do is set a cookie that when a user visits url.com/mens home for them will be url.com/mens. When they visit url.com/womens, home will be url.com/womens.

    The idea is that if they visit url.com/womens, I can assume they are probably a female, and their homepage should just feature female-specific content.

    The header has a mens/womens toggle button, so they can easily navigate between the two.

    Is this possible/feasible?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Yes it is possible / feasible.

    Custom code of course.

    Use the ‘init’ hook to set the cookie, if not set.

    Use the ‘template-redirect’ hook to inspect the cookie if set and redirect if landing on ‘real’ home to ‘temporary home’

    Thread Starter oguruma

    (@oguruma)

    @fullworks alright, so that all makes sense, but are redirects the ideal way to this? If I understand what you’re saying, I would do something like

    if ( is_front_page() ) {
            wp_redirect( home_url( '/mens/' ) );

    So “home” icons would still take them to url.com, but they would still be re-directed to url.com/mens.

    Is there a way to, without redirecting, changing the url of home_url?

    The redirect happens before the page is loaded on so it is not really a redirect in that sense. i.e. it will just happen not be a load / reload ( don’t forget the exit after wp_redirect – also i’d use wp_safe_redirect )

    Thats the way I’d do it ( and do in a similar way my 404 redirect plugin for instance )

    You can’t update the options table for site url and that will impact every one.

    You cant filter options_siteurl or options_home as that will impact many things that you could not control and would be a mess.

    The alternative is for you code the logic at theme level, for instance in the header where it displays the logo with a home link apply the cookie lookup and in the menu apply logic too – there is a plugin that allows using shortcodes in menu items so you could write a simple shortcode to check the cookie.

    Alternatively, if you are so inclined, you could examine the cookie in htaccess
    e.g.

    RewriteEngine On
    RewriteCond %{HTTP_COOKIE} !cookie_name=specific_value; [NC]
    RewriteRule ...
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change homepage based on cookie?’ is closed to new replies.