繁体   English   中英

如何在WordPress中登录到BuddyPress配置文件后重定向

[英]How to redirect after login to BuddyPress profile in WordPress

我用BuddyPress插件运行WordPress 5.1.1。

基本上我想要做的是,将用户从自定义登录链接重定向到BuddyPress中的个人资料页面。

我已经阅读并检查了Stackoverflow上提供的几乎所有代码以查找我的类似查询,但是没有人在我的网站上工作过。

唯一的代码在下面工作,但它对我的网站设置和设置有一个问题。

function bp_help_redirect_to_profile(){
  global $bp;
  if( is_user_logged_in() && is_front_page() ) {
   bp_core_redirect( get_option('home') . '/members/' . 
   bp_core_get_username( bp_loggedin_user_id() ) . '/profile' );
  }
}
add_action( 'get_header', 'bp_help_redirect_to_profile',1);

问题是,当我想在网站的主页上重定向时,它会继续将我重定向到BuddyPress配置文件。 类别和帖子部分正确加载。

所以,我需要的是,当登录流程重定向用户在他们的BuddyPress个人资料页面上,当用户点击网站的主页加载网站的主页而不是BuddyPress。

我希望有人可以通过这种方式帮助和调整功能。

谢谢

您可以使用wp_login操作。 请查看以下示例。 bp_core_get_user_domain()将获取配置文件URL,成功登录后,用户将被重定向到该配置文件URL。

function wpso_take_to_profile( $user_login, $user ) {
    if ( function_exists( 'bp_core_get_user_domain' ) ) {
        $url = bp_core_get_user_domain( $user->ID );
        wp_redirect( $url );
        die();
    }
}
add_action('wp_login', 'wpso_take_to_profile', 10, 2);
add_filter( 'bp_login_redirect', 'bpdev_redirect_to_profile', 11, 3 );

function bpdev_redirect_to_profile( $redirect_to_calculated, $redirect_url_specified, $user )
{

  if( empty( $redirect_to_calculated ) )
    $redirect_to_calculated = admin_url();

    //if the user is not site admin,redirect to his/her profile

if( isset( $user->ID) && ! is_super_admin( $user->ID ) )
    return bp_core_get_user_domain( $user->ID );
else
    return $redirect_to_calculated; /*if site admin or not logged in,do not do 
    anything much*/

}

测试代码放在您的活动主题功能文件中

暂无
暂无

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

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