BuddyDev

Extending BuddyPress profile field visibility

BuddyPress profile field visibility allows users on a BuddyPress based social site to change the visibility of individual profile field data. It controls who can see a profile filed data of the user.

Default Visibility Levels:-

By default, BuddyPress provides four visibility levels:-

  • Everyone:- Anyone can see the data.
  • Only Me:- Only the user and the site admin can see the data
  • All Members:- Anyone logged into the site can see the data
  • My Friends:- This visibility level is only available if the Friends component is enabled. A user can mark the filed only visible t his/her friends.

Today, In this tutorial, we will learn how to add new profile field visibility level as well as how to remove existing visibility levels.

It is a long tutorial, If you are looking for the code, you can directly head to github and download it from the below link.

 https://github.com/buddydev/extended-xprofile-field-visibility-levels

If you are here, I promise you are going to learn a lot. I will illustrate the tutorial with the help of an example that creates a new privacy level "My Group Members". A field marked as My Group Members will be only visible to users who have at least 1 common group with this user.

BuddyPress provides a hook 'bp_xprofile_get_visibility_levels' that allows us to add new privacy level.  Any function that is hooked to this filter can add a new visibility level or remove any existing level.

Here is an example syntax.

In the above code, we have hooked 'buddydev_customize_profile_visibility' to the filter. Now, let us see an implementation of the 'buddydev_customize_profile_visibility' function.

A sample implementation will look like the above. The hooked function gets all the existing visibility levels as a multi dimensional associative array as shown below.

As you can see, the keys  publicadminsonly,  loggedin, friends are used to identify each of the existing visibility level.

Removing Existing Visibility Level:-

In case you want to remove an existing visibility level, you can use

Where the Identifier key can be one of the following:-

  • public
  • adminsonly
  • loggedin
  • friends

Let us see a real example, to unset the My Friends visibility level, we can use the following code inside our function.

Once you do that you will see that the My Friends visibility is gone as shown in the screenshot below.

disableing-friends-visibility

Adding a New Visibility Level:-

Building upon our previous example, we already know that a new privacy level can be added with a new key and It's levels. Let us add My Group Members to the visibility lebel.

In the above code, we are testing if the groups component is enabled and if yes, we add the new privacy level. I hope that the example makes it clear what we are doing. After putting this code, you can check that the new Visibility level is added as shown in the screenshot below.

 

And here is the complete implementation of the hooked function

 

But wait, that's not all. Till now, we have told BuddyPress that please add this new privacy level but we have not specified how the visibility will actually contol the data. In other words, we need to tell BuddyPress when to hide a field with our new visibility and when to show it.

BuddyPress provides another hook for the purpose. We will use 'bp_xprofile_get_hidden_field_types_for_user' to tell BuddyPress when to hide our field data. Please don't get fooled by the name of the hook, It is not about profile field types, It is about profile field visibility and a better name should have been 'bp_xprofile_get_hidden_field_visibility_levels_for_user'. But that is not our job, so let us use 'bp_xprofile_get_hidden_field_types_for_user' and hook a function to tell BuddyPress when the fields with 'groups' visibility be hidden.

Here is what we want to accomplish:-

  • If the profile data is fetched for the logged in user, we should not restrict as he/she is the owner of the profile
  • If the logged in user is super admin, do not hide
  • If the logged in user is having a common group with the user whose profile data is being fetched, then do not hide it
  • In all other cases, just hide it.

Here is a code to do that for groups privacy.

That does it. As you might have noticed, I have used a function 'buddydev_user_has_common_group' which does not exist yet. Well, BuddyPress does not provide a built in way to find common groups of two users, so we will write our own to find this. I have broken the code into two functions, so if you want, you can use a part of it to fetch common groups between any user.

Everything is setup and you can have some fun testing it. Yo can put the above code in your bp-custom.php

Here is the list of interesting things you can do with the knowledge you earned today.

  • A visibility level for Sent Pending Friendship Request:- Allow users to make some fields visibility only to the members whom they have sent request( Like Friends + Users whom the profile user sent requst)
  • Followers only visibility
  • Following Only visibility
  • Gender based visibility (  I have a lot to talk about this in our upcoming posts).
  • and what ever you can think.

If you want me to add any specific visibility to the sample plugin, Please let me know your suggestions. I am looking forward to  your thoughts, suggestions and criticism all in the comments below.

Happy social networking 🙂

 

16 Responses to Extending BuddyPress profile field visibility

  • Hello,

    I just wanted to say thanks for this code. I was specifically interested in the 'common groups' part that you added. Very nice. I tested it on my dev site and it works great. Now I'll look to add to our live site.

    Thanks.

  • Hello,
    Thank you for this inspiring piece of code. We need to hide various x-profile fields for a 2 user roles. Could you tell me if this is possible with this code. We tried but failed up to now

    • Hi Michael,
      can you please link me to the code(pastebin/github). Yes, It will allow you to work with roles . I have even done the same for one of my clients for specific member types.

      • Hello! I would love to see how to establish a privacy level to work with user "Types" or roles. Any help great appreciated!

        • Any luck with this "privacy level to work with user “Types” or roles"

  • Hi Brajesh,

    I'm a real Dummy at PHP programming. To be honest i'm more of a code copymaster 😉
    So I tried some copying and pasting of code.
    And after a couple of days and headaches something came out that really seems to work.

    But since it's for a real website (and i'm the Dummy) I was wondering if you could take a look at https://github.com/IChess1/WPI/tree/master.
    I've put your code there with my added code in it.
    What do you think?

  • Worked beautifully, thank you!!

  • Any update on Gender based visibility ( I have a lot to talk about this in our upcoming posts).

  • Some of my visitors want to hide some of their fields from males, is it possible.?

  • This is great! I'm a novice at this, and this was hoping you could help me understand how to customize this to add a visibility specification for a certain buddypress group. Thanks!

    • Hi Marshal,
      Please post in our forums and we will help.

      Thank you.

  • Is there a plugin that does this? This functionality is quite important to me.