WordPress Cookie Redirect – Home Page to Blog Page

I’m using WordPress and I want the user to go to the Home Page on their first visit, but on every other visit after that I would like them to be redirected to the Blog.

Home Page:

Read More
www.website.com

Blog:

www.website.com/blog

I’m guessing the best way to do this is to set a cookie?

I have no idea on what PHP files to edit or anything…

Related posts

1 comment

  1. In your theme functions.php ( or plugin )

    function o99_set_newvisitor_cookie() {
        if ( !is_admin() && !isset($_COOKIE['sitename_newvisitor'])) {
            setcookie('sitename_newvisitor', 1, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false);
        }
    }
    add_action( 'init', 'o99_set_newvisitor_cookie');
    

    After that

    if (isset($_COOKIE['sitename_newvisitor'])) {
         echo 'Welcome back!'; // or redirect using wp_redirect( 'some_url/' ); exit;
    }
    else {
         echo 'Hello new visitor!'; // or redirect using wp_redirect( home_url() ); exit;
    }
    

    This should do the job .

    WordPress itself had a function called wp_setcookie() but it was deprecated and replaced by wp_set_auth_cookie() which is only for user auth I believe . Not sure why, but maybe because of cookies laws that were introduced ( and that also you need to take into account )

    Anyhow, see also the normal PHP setcookie() docs and the wp_redierct() function in codex.

Comments are closed.