• Resolved hasankassem

    (@hasankassem)


    Hi. I’m relatively new to wordpress and generally unfamiliar with php code; I was wondering however if there is a way to use multiple homepages. Im using a static home page, but I want the page to flip to a different page if a visitor has already been there. For example, if I visit the website for the first time , I am greeted with page A. Somehow, the website memorizes my visit (through cookies perhaps). Next time I visit the website, or if I navigate through it and then wind up back on the homepage, it redirects me to a different home page (or just changes the set home page entirely).
    Is this in anyway possible? Thanks in advance.

    • This topic was modified 3 years, 8 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 4 replies - 1 through 4 (of 4 total)
  • This can be done using the WordPress hook template_redirect and the PHP function setcookie().

    Place this code in your functions.php file or use the Code Snippets plugin. Replace secondary-homepage with the slug of the page you create for subsequent visits. You can also replace WEEK_IN_SECONDS with any of these:

    • MINUTE_IN_SECONDS
    • HOUR_IN_SECONDS
    • DAY_IN_SECONDS
    • MONTH_IN_SECONDS
    • YEAR_IN_SECONDS
    add_action('template_redirect', 'wpsf_multiple_homepages');
    function wpsf_multiple_homepages()
    {
      // Visitor arrives at front page
      if (is_front_page()) {
    
        // It's their first visit
        if (!isset($_COOKIE['wpsf-homepage'])) {
    
          // Remember that they've visited
          setcookie('wpsf-homepage', 'visited', time() + WEEK_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, true);
          return;
        }
    
        // On subsequent visits...
        if (isset($_COOKIE['wpsf-homepage'])) {
    
          // Send visitor to another page
          wp_safe_redirect(home_url('secondary-homepage'));
          exit;
        }
      }
    }
    Thread Starter hasankassem

    (@hasankassem)

    @rickymccallum87 Thank you for the assistance. However, for some reason, the code is not working. Upon entering the homepage, the browser doesnt seem to see the cookie (I checked through the console, but did not find a cookie with that name there). I am working on a local setup, if thats relevant somehow.
    Another question: Assuming I get the code to work, how can I get the pages to alternate? So I enter the website for the first time, loading page A. COokie is created. On the second visit, it checks for the cookie and redirects me to Homepage B. Is it possible to delete the cookie with the redirect, thus ensuring that on a visitor’s third visit, they will be taken back to page A? Making the user effectively cycle through both homepages based on the one last visited. That is my intention all along; apologies that that was not clear in my original question.

    The final parameter in the setcookie() function is true for secure HTTPS connections. Switching that to false temporarily will probably get it working for your local install.

    Alternating homepages is a bit more complicated. I’ve set this up how you described: setting the cookie on the first visit and expiring it on the second.

    add_action('template_redirect', 'wpsf_alternating_homepage');
    function wpsf_alternating_homepage()
    {
      // Visitor arrives at front page
      if (is_front_page()) {
    
        // It's their first visit (or an odd-numbered visit)
        if (!isset($_COOKIE['wpsf-homepage'])) {
    
          // Remember that they've visited
          setcookie('wpsf-homepage', 'visited', time() + WEEK_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN, false);
          return;
        }
    
        // It's their second visit (or an even-numbered visit)
        if (isset($_COOKIE['wpsf-homepage'])) {
    
          // Forget visitor by expiring the cookie
          setcookie('wpsf-homepage', 'visited', 1, SITECOOKIEPATH, COOKIE_DOMAIN, false);
    
          // Send visitor to the other front page
          wp_safe_redirect(home_url('secondary-homepage'));
          exit;
        }
      }
    }
    Thread Starter hasankassem

    (@hasankassem)

    @rickymccallum87 Many thanks for your time; struggled with this for almost a week now, so it is much appreciated. All is working fine and dandy now.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Multiple Homepages’ is closed to new replies.