简体   繁体   English

WordPress Cookie重定向-主页到博客页面

[英]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. 我正在使用WordPress,希望用户在第一次访问时就转到主页,但是在此之后的所有其他访问中,我希望将他们重定向到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? 我猜最好的方法是设置一个cookie?

I have no idea on what PHP files to edit or anything... 我不知道要编辑哪些PHP文件或其他任何内容...

In your theme functions.php ( or plugin ) 在您的主题functions.php(或插件)中

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 . Wordpress本身具有一个名为wp_setcookie()的函数,但已弃用并替换为wp_set_auth_cookie() ,我相信它仅用于用户身份验证。 Not sure why, but maybe because of cookies laws that were introduced ( and that also you need to take into account ) 不知道为什么,但是可能是因为引入了cookie法律(并且您还需要考虑)

Anyhow, see also the normal PHP setcookie() docs and the wp_redierct() function in codex. 无论如何,另请参见正常的PHP setcookie()文档和Codex中的wp_redierct()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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