BuddyDev

Search

Profile Visibility for non-logged in users

  • Participant
    Level: Enlightened
    Posts: 38
    Keith on #27776

    I’m using the code snippet to hide profile fields using member types, like the one in this post, https://buddydev.com/using-buddypress-member-types-as-profile-field-visibility-levels/. But it doesn’t work on users who are not logged in. How can I hide the fields from non-logged in users as well?

  • Participant
    Level: Enlightened
    Posts: 38
    Keith on #27777

    Here is the code I used in my bp-custom.php

    //Add visibilty by member type
    add_filter( 'bp_xprofile_get_visibility_levels', 'buddydev_add_member_type_profile_visibility' );
    
    function buddydev_add_member_type_profile_visibility( $visibilities ) {
     
        $member_types = bp_get_member_types( array(), 'object' );
        // If there is no registered member type, return the original visibilities.
        if ( empty( $member_types ) ) {
            return $visibilities;
        }
     
        // Add our member type as visibility levels.
        foreach ( $member_types as $member_type => $member_type_object ) {
            $visibilities[ $member_type ] = array(
                'id'    => $member_type,
                'label' => $member_type_object->labels['singular_name'],
            );
        }
     
        return $visibilities;
    }
    
    function buddydev_get_hidden_visibility_types_for_user( $hidden_levels, $displayed_user_id, $current_user_id ) {
     
        $member_types = bp_get_member_types();
     
        // If user is not logged in
        // All member type visibilities will be hidden.
        if ( ! is_user_logged_in() ) {
            $hidden_levels = array_merge( $hidden_levels, $member_types );
        } elseif ( ( $displayed_user_id == $current_user_id ) || is_super_admin( $current_user_id ) ) {
            // Do not hide anything on own profile or viewed by super admin.
        } else {
            // User is logged in and viewing other user's profile.
            $logged_member_types = bp_get_member_type( $current_user_id, false );
            // except current user's member type, all other levels will be hidden.
            if ( $logged_member_types ) {
                $member_types = array_diff( $member_types, $logged_member_types );
            }
     
            $hidden_levels = array_merge( $hidden_levels, $member_types );
        }
     
        return $hidden_levels;
    }
     
    add_filter( 'bp_xprofile_get_hidden_field_types_for_user', 'buddydev_get_hidden_visibility_types_for_user', 10, 3 );
  • Participant
    Level: Enlightened
    Posts: 38
    Keith on #27778

    Disregard, It’s only not working on one members profile. I’m not sure why.

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #27782

    Hi Keith,
    On a cursory look, It seems all good to me.

    Regards
    Brajesh

The topic ‘Profile Visibility for non-logged in users’ is closed to new replies.

This topic is: not resolved