覆盖wordpress登录重定向功能


Override wordpress login redirect function

我在自定义插件中存储了一个登录函数,它覆盖了标准的Wordpress登录,一旦用户登录,将他们重定向到所需的页面:

function admin_login_redirect( $redirect_to, $request, $user )
{
global $user;
if( isset( $user->roles ) && is_array( $user->roles ) ) {
if( in_array( "administrator", $user->roles ) ) {
return $redirect_to;
} else {
return home_url();
}
}
else
{
return $redirect_to;
}
}
add_filter("login_redirect", "admin_login_redirect", 10, 3);

这是一个治疗,但我现在需要建立一个额外的/替代登录页面,如果使用,重定向用户到不同的登陆页面。

似乎我只有三个合乎逻辑的选择:

  • 在新的登录页面中编写代码以忽略插件重定向功能(这甚至是可能的!?)
  • 识别用户正在使用的登录模板/表单,并将此逻辑构建到上述函数或
  • 在新的/可选的登录页面中重载重定向功能(上面)

有没有人知道如果这是可能的,如果是,我该怎么做?

我认为最好的选择是创建一个新的登录页面,并在该页面的登录操作后定义重定向。

自定义登录页面的页面模板看起来像这样:

<?php
    // Define custom redirect
    $custom_redirect = "REDIRECT_URL";
    // If user is logged out
    if ( !is_user_logged_in() ) {
        $args = array(
            'redirect' => $custom_redirect
        );
        // Output the login form with arguments
        wp_login_form( $args );
    } 
    // Display below if user is logged in
    else {
        wp_loginout( home_url() ); // Display "Log Out" link.
        echo " | ";
        wp_register('', ''); // Display "Site Admin" link.
    }
?>

你可以将它包装在functions.php中的custom_login_form()函数中,然后在自定义登录页面模板中调用该函数,或者直接将其放置在自定义登录页面模板中。您可以将自定义页面模板命名为page-login.php,它将自动应用于名为"login"的WP页面。

为登录页面创建自定义页面模板的完整指南。

自定义登录表单的完整指南。