gpt4 book ai didi

php - 覆盖 wordpress 登录重定向功能

转载 作者:搜寻专家 更新时间:2023-10-31 21:51:52 24 4
gpt4 key购买 nike

我有一个存储在自定义插件中的登录功能,它覆盖了标准的 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 页面。

Full guide on creating a custom page template for the login page .

Full guide on customizing the login form .

关于php - 覆盖 wordpress 登录重定向功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40335642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com