【问题标题】:Attempting to Redirect to User Profile尝试重定向到用户配置文件
【发布时间】:2015-09-23 21:04:34
【问题描述】:

使用 BuddyPress,我试图将所有注册用户重定向到他们的用户资料;但是,当重定向生成的 URL 时不包括 user_login... 因此每个用户都重定向到“域/成员/”。

如何让用户名显示在 URL 中,然后重定向到以下内容:“域/成员/用户名/”?

FUNCTIONS.PHP

add_filter('login_redirect', function ($redirect_to, $request, $user){
    if (user_can($user, 'registered')){
      $current_user = wp_get_current_user();
      return '/members/' . $current_user->user_login . '/';
    }
}

【问题讨论】:

    标签: php wordpress redirect buddypress


    【解决方案1】:
        function redirect_to($new_location) {
          header("Location: " . $new_location);
          exit;
        } 
    

    重定向函数将此函数称为redirect_to("redirect_page");

    【讨论】:

    • 使用 WordPress,而不是纯 PHP。
    【解决方案2】:

    一种不依赖硬编码 slug 的更好方法:

    return bp_core_get_user_domain( $user->ID );
    

    【讨论】:

    • 感谢核心功能。
    【解决方案3】:

    如 WordPress Codex 中所述:“$current_user 全局可能在此过滤器运行时不可用。因此您应该使用 $user 全局或传递给此过滤器的 $user 参数。”

    https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect

    add_filter('login_redirect', function ($redirect_to, $request, $user){
        if (user_can($user, 'registered')){
            return bp_core_get_user_domain( $user->ID );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 2021-02-18
      相关资源
      最近更新 更多