Skip to:
Content
Pages
Categories
Search
Top
Bottom

Redirect to current profile when access others


  • ayllachristinne
    Participant

    @ayllachristinne

    Hi, I want to show the loggedin user profile if he/she tries to access other user’s profile.
    Here’s a list of what I want and what I’ve accomplished

    • OK – Redirect to own profile when entering /members/
    • X – Redirect to own profile when entering another profile via link
    • OK – WMDs can view member listings
    • OK – ADMs can view member profile

    This is the code i have until now

    function buddydev_hide_members_directory_for_all_except_users_with_specific_roles() {
      if ( ! bp_is_members_directory() ) {
        return;
      }
    
      if ( !is_user_logged_in() ) {
        bp_do_404();
        load_template( get_404_template() );
        exit( 0 );
      }
    
      $allowed_roles = array( 'administrator', 'editor' );
      $hide          = true;
      $user          = wp_get_current_user();
      
      foreach ( $allowed_roles as $role ) {
        if ( in_array( $role, $user->roles, true ) ) {
          $hide = false;
          break;
        }
      }
    
      if ( ! $hide ) {
        return;
      }
    
      /** 
       * bp_is_my_profile()
       * bp_is_user_profile()
       * bp_is_user()
       * bbp_is_single_user_profile()
       * bbp_is_user_home()
       * bbp_is_single_user()
       */
       if(!bbp_is_user_home()){
           wp_redirect(bp_loggedin_user_domain());
       }
    
      //REDIRECT TO USER PROFILE
      wp_redirect(bp_loggedin_user_domain());
      exit( 0 );
    }
    add_action( 'bp_template_redirect', 'buddydev_hide_members_directory_for_all_except_users_with_specific_roles' );

    I tried some things like the last IF with the commented functions but none work, so I guess I’m doing something wrong. Can you help me plz?

    Thanks in advance

Viewing 1 replies (of 1 total)

  • ayllachristinne
    Participant

    @ayllachristinne

    Thats the code:
    function buddydev_hide_members() {
    $allowed_roles = array( ‘administrator’, ‘editor’ );
    $hide = true;
    $user = wp_get_current_user();

    if(str_contains($_SERVER[‘REQUEST_URI’], ‘/membros/’)){
    //REDIRECIONA PARA LOGIN SE NÃO ESTIVER LOGADO
    if ( !is_user_logged_in() ) {
    wp_safe_redirect(get_permalink(get_page_by_path(‘login-registro’)));
    exit;
    }

    //CHECANDO SE TEM O PAPEL QUE PODE VISUALIZAR
    foreach ( $allowed_roles as $role ) {
    if ( in_array( $role, $user->roles, true ) ) {
    $hide = false;
    break;
    }
    }

    if( ! $hide ){
    return;
    } elseif(!bbp_is_user_home()) {
    wp_safe_redirect(bp_loggedin_user_domain());
    exit;
    }
    }
    }
    add_action( ‘bp_template_redirect’, ‘buddydev_hide_members’);

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.
Skip to toolbar