BuddyDev

Search

[Resolved] Buddypress xprofile visibility option (For Buddypress follow plugin)

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #51265

    Hi,

    Please how can I add a new xprofile visibility option to buddypress

    This is a rough attempt below but not working

     // Add 'My Followers' profile visibility option to xprofile fields
    add_filter('bp_xprofile_get_visibility_levels', 'add_followers_visibility_option');
    
    function add_followers_visibility_option($levels) {
        if (function_exists('bp_follow_is_following')) {
            $levels['followers'] = __('My Followers', 'klein'); // Adjust 'klein' to your theme/plugin text domain
        }
        return $levels;
    }
    
    // Set profile visibility based on followers
    add_filter('bp_xprofile_is_profile_data_public', 'set_profile_visibility_based_on_followers', 10, 3);
    
    function set_profile_visibility_based_on_followers($is_public, $field, $user_id) {
        if ($field->type == 'visibility' && $field->data->value == 'followers' && function_exists('bp_follow_is_following')) {
            $current_user_id = get_current_user_id(); // Get the ID of the currently logged-in user
            if ($current_user_id) {
                $is_following = bp_follow_is_following($user_id, $current_user_id); // Check if current user is following the user
                $is_public = $is_following; // Set the profile field to be visible to the current user's followers
            }
        }
        return $is_public;
    } 
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #51267

    Hello Tosin,

    Please check the following blog post about extending profile visibility options

    https://buddydev.com/extending-buddypress-profile-field-visibility/

    Regards
    Ravi

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #51271

    Thank you Ravi,

    This code below worked for me

     function custom_modify_visibility_levels($allowed_visibilities) {
        // Add a new visibility level for 'My Followers'
        if (function_exists('bp_follow_is_following')) {
            $allowed_visibilities['followers'] = array(
                'id'    => 'followers',
                'label' => __('My Followers', 'buddypress')
            );
        }
    
        return $allowed_visibilities;
    }
    add_filter('bp_xprofile_get_visibility_levels', 'custom_modify_visibility_levels');
    
    // Adjust visibility based on followers
    function custom_get_hidden_visibility_types_for_user($hidden_levels, $displayed_user_id, $current_user_id) {
        if ($displayed_user_id != $current_user_id && !bp_follow_is_following(array('leader_id' => $displayed_user_id, 'follower_id' => $current_user_id)) && !is_super_admin()) {
            $hidden_levels[] = 'followers'; // Hide profile field with 'My Followers' visibility level for the user
        }
        return $hidden_levels;
    }
    add_filter('bp_xprofile_get_hidden_field_types_for_user', 'custom_get_hidden_visibility_types_for_user', 10, 3); 
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #51281

    Hello Tosin,

    Thank you for the acknowledgement and for sharing the working code with us.

    Regards
    Ravi

You must be logged in to reply to this topic.

This topic is: resolved