简体   繁体   中英

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:

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM