简体   繁体   English

如何将注销的用户重定向到WordPress中的其他页面

[英]How to redirect logged out users to different page in WordPress

I am creating a website using WordPress. 我正在使用WordPress创建网站。 I am new to it. 我是新来的。 I have a separate homepage for logged out users and logged in users. 我有一个单独的主页,用于注销用户和登录用户。 Page for logged out users tells them to sign up or log in. And homepage for logged in users show them there profile. 已注销用户的页面告诉他们注册或登录。已登录用户的主页显示他们的个人资料。 So the index.php is the homepage for logged out users. 因此index.php是注销用户的主页。 And i have created a different page freevideofy.com/home for logged in homepage. 我创建了另一个页面freevideofy.com/home作为登录主页。

I use plugin - login redirect to redirect them to freevideofy.com/home when they login in or sign up. 我使用插件-登录重定向将他们登录或注册时重定向到freevideofy.com/home And i use plugin - logout redirect to redirect them to freevideofy.com when they logout. 我使用插件-注销重定向将其注销时重定向到freevideofy.com But the problem is, if a logged in user closes my website, and again opens it by url - freevideofy.com then they are not automatically redirected to freevideofy.com/home . 但是问题是,如果登录的用户关闭了我的网站,然后再次通过url-freevideofy.com来打开它,则他们不会自动重定向到freevideofy.com/home

So I want if logged in user opens the url - freevideofy.com, then he should be redirected to freevideofy.com/home. 因此,我想如果登录的用户打开URL-freevideofy.com,则应将他重定向到freevideofy.com/home。 I think this function can be used - is_user_logged_in 我认为可以使用此功能is_user_logged_in

As you said you didn't used any page builder or set a page as FrontPage ( using settings ), Wordpress should load your theme index.php file as homepage. 正如您所说的,您没有使用任何页面构建器或将页面设置为FrontPage(使用settings),Wordpress应该将主题index.php文件加载为主页。 So you have to edit wp-content/themes/THEMENAME/index.php file and add these two lines of codes: 因此,您必须编辑wp-content / themes / THEMENAME / index.php文件并添加以下两行代码:

if (is_user_logged_in() && is_front_page()) {
    wp_redirect(home_url('/home'));
}

If you feel a bit more streaky to edit the codes you can use the plugins for redirection also. 如果您觉得编辑代码有些花哨,也可以使用插件进行重定向。 Since WordPress uses enormous plugins that reduces our work and so i would suggest you to use these plugins for redirection. 由于WordPress使用大量的插件,减少了我们的工作,因此我建议您使用这些插件进行重定向。

Plugin Name 1: Peter's Login Redirect 插件名称1:Peter的登录重定向

Redirect users to different locations after logging in and logging out. 登录和注销后,将用户重定向到其他位置。

Plugin URL: https://wordpress.org/plugins/peters-login-redirect/ 插件网址: https//wordpress.org/plugins/peters-login-redirect/

Plugin Name 2: Redirection 插件名称2:重定向

Redirection is a WordPress plugin to manage 301 redirections and keep track of 404 errors without requiring knowledge of Apache .htaccess files. 重定向是一个WordPress插件,可以管理301重定向并跟踪404错误,而无需了解Apache .htaccess文件。

Plugin URL: https://wordpress.org/plugins/redirection/ 插件网址: https//wordpress.org/plugins/redirection/

Plugin Name 3: Redirect After Login 插件名称3:登录后重定向

Redirect based on Role here. 根据此处的角色进行重定向。

Plugin URL: https://wordpress.org/plugins/redirect-after-login/ 插件网址: https : //wordpress.org/plugins/redirect-after-login/

Hope so this will be the easiest method to do redirection based on the login roles and login status as such. 希望如此,这将是基于登录角色和登录状态的最简单的重定向方法。

If so you need to do it via function in the WordPress you can do it better in this way. 如果是这样,您需要通过WordPress中的函数进行操作,则可以通过这种方法做得更好。

Function Name: wp_redirect() 函数名称:wp_redirect()

Description: Redirects to another page. 说明:重定向到另一个页面。

Note: wp_redirect() does not exit automatically, and should almost always be followed by a call to exit; 注意: wp_redirect()不会自动退出,并且应该几乎总是在其后调用退出;

<?php
if(is_user_logged_in())
{
wp_redirect( home_url('/home') ); // This will redirect to the home page which we have created.
exit;
}
else
{
wp_redirect( home_url() );// this will redirect to the site main home page.
exit; 
}
?>

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

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