访问时重定向/登录时重定向

时间:2017-10-03 作者:Tom

我正在使用Theme My Login插件将登录页面从“/wp Login.php”更改为“/Login”。

我的问题是,如何重定向已经登录的用户,以便访问“/登录”将他们带到我的主页?

注意:我说的不是登录后的初始重定向,而是如果有人在登录后再次访问登录页面。

我在这里看到了一个类似的问题:wp-login.php — redirect logged in users to custom URL

我尝试使用张贴在那里的代码,并将其更改为:

function redirect_logged_in_user() 
{
    if( is_user_logged_in ) {
      wp_redirect(\'http://my_homepage_url\');
    }
}

global $pagenow;
if( $pagenow == \'/login\')     
redirect_logged_in_user();
但它仍然不起作用。

任何帮助都将不胜感激。

干杯

2 个回复
SO网友:Temani Afif

我建议你试试这个,更好的行动login_init:

add_action(\'login_init\', \'redirect_logged_in_user\');
function redirect_logged_in_user()
{
    global $action;

    /* if the user call logout and is not logged in we do nothing*/
    if (\'logout\' === $action || !is_user_logged_in()) {
        return;
    }

    /* we redirect logged in people*/
    wp_redirect(\'http://my_homepage_url\');
    exit;
}

SO网友:Tom

    add_action(\'wp\', \'add_login_check\');
function add_login_check()
{
    if ( is_user_logged_in() && is_page( [6070, 6072] ) ) {
        wp_redirect(\'http://my_homepage_url\');
        exit;
    }
}
我从这里获取了代码:https://stackoverflow.com/a/25992092

然后我移动了“add\\u action(\'wp\',add\\u login\\u check\');”在顶部添加另一个“is\\U页”(6070=登录页ID,6072=注册页ID)。

到目前为止,它似乎一直有效,允许我注销。

这段代码写得好吗?有必要改变什么吗?

结束

相关推荐

Enqueue new login style sheet

我尝试将一个名为login\\u styles的新登录样式表排队。css。这是我使用的代码:function my_logincustomCSSfile() { wp_enqueue_style(\'login-styles\', get_template_directory_uri() . \'/login_styles.css\'); } add_action(\'login_enqueue_scripts\', \'my_logincustomCSSfile\');&