xProfile fields only visible to specific role?

Hi,

I use xProfiles in Buddypress along with the members plugin. I also have 3 roles;

visitor
client
expert

I added an xprofile group with a few fields. I want 'expert' role members to see and add to these fields but I don't want other roles to be able to see these fields. I found a way to show/hide based on role in the group members loop but I can't figure out how to hide the group (or fields) in the main profile page for the member.

It looks like the ACL module would have done it but it's not been updated in over 2 years and now it doesn't seem to work at all.

I'm on MU with the latest updates on everything. Any help on a hack to hide this group (or fields) from the profile or maybe a fix to make the ACL plugin work would be very much appreciated.

Thanks a bunch!

PS - second month paying and now on $99 monthly so really hoping you can help :wink:

  • Tyler Postle
    • CGO

    Glad you decided to go another Month Lyndon! Very sorry we haven't gotten back to you sooner.

    Would you mind uploading some screenshots of what you want to hide? Just so I can get a better idea of what is happening here.

    Another staff, Alexander, posted a possible code solution on this thread: https://premium.wpmudev.org/forums/topic/membership-how-to-hide-buddypress-xprofile-fields would you mind checking if that code works?

    Let us know, if not, then we can continue troubleshooting :slight_smile:

    Cheers,
    Tyler

  • Lyndon
    • Site Builder, Child of Zeus

    Hi @Tyler - I had seen that post thanks. It's not really clear how to make use of the code in the post or even where to put it. I can normally fudge my way around editing basic PHP - but - from this code;

    add_filter('xprofile_get_field_data','protect_xprofile');
    
    function protect_xprofile($value) {
    return (current_user_on_level(1)) ? $value : "protected";
    }

    Questions.
    1) Which file does this code go in to?
    2) Where do I define which xprofile fields or groups the function hides? I don't want to hide all xprofile data, just some fields within a particular group I created.

    I haven't looked yet but I'm guessing I can work out the user/member level.

    Thanks

  • Tyler Postle
    • CGO

    Hey Lyndon,

    You would have to search around for membership functions to make use of it. Perhaps the second option might be better to try out?

    In that case, let's see if we can get the URL groups to workout, as this is a great idea. For a wildcard URL group, you could use this: http://yoursite.com/members/.*/profile/edit/group/2/

    You have likely already seen the URL groups setting in your membership options. If you place the wildcard URL group above then you can protect all the URL's with one string.

    Here is a video that illustrates how to protect URL's using Membership: http://youtu.be/nVepd0HGOh8

    Let us know if this works :slight_smile: If not, we'll continue looking for a solution!

    Cheers,
    Tyler

  • Lyndon
    • Site Builder, Child of Zeus

    Hi @Tyler Postle - The URL solution is not good for me. The profile data I wish to hide is on the same profile page as the profile data I wish to display.

    I'm not sure what you mean by "You would have to search around for membership functions to make use of it.".

    Are you able to help understand how to use the code suggested? Perhaps you could try to answer the 2 direct questions I posted please?

  • Tyler Postle
    • CGO

    Hey again Lyndon,

    Sorry my last post wasn't of much help to you. After thinking about it a bit more, I can see how using membership URL groups wouldn't be the best way to go!

    I'm not the PHP expert around here, so I would likely just make you more lost trying to implement that strategy; however, I will call in @Hoang Ngo, when he has a free moment, he is a lot more adept at PHP than myself.

    In the meantime, I did find an article that goes into more detail on implementing PHP; however, keep in mind that it is a couple years old now. Still be worth looking at for the time being. Let us know if anything changes! https://premium.wpmudev.org/forums/topic/how-to-hide-specific-buddypress-profile-fields

    All the best,
    Tyler

    PS. Can you also grant support access in-case it is needed? This will save time in the future. You can do this via the dashboard WPMU Dev > Support > Support Access > Grant Access.

  • Lyndon
    • Site Builder, Child of Zeus

    Thanks @Tyler Postle - I'll look through that code in the link. However, they seem to talk about difficulties because they didn't want to hide an entire group - I do so is what I want easier? If so, maybe you or @Hoang Ngo can help with that. I think that could be cleaner because I actually only want to hide the xprofile group in question from some roles/membership levels and not from everyone.

    Thanks

  • Hoang Ngo
    • Code Slayer

    Hi @Lyndon,

    I hope you are well today and thanks for your question.

    Please add this code to your theme functions.php or use mu-plugins

    add_filter( 'bp_after_has_profile_parse_args', 'hide_profile_group' );
    function hide_profile_group( $args ) {
    	$exclude_rules  = array(
    		'Level id' => 'group ids, separate by comma',
    	);
    	$exclude_groups = '';
    	foreach ( $exclude_rules as $key => $val ) {
    		if ( current_user_on_level( $key ) ) {
    			$exclude_groups = $val;
    			break;
    		}
    	}
    	if(!empty($exclude_groups)){
    		$args['exclude_groups']=$exclude_groups;
    	}
    	return $args;
    }

    The code you need to update is

    $exclude_rules  = array(
    		'Level id 1' => 'group ids, separate by comma',
    'Level id 2' => 'group ids, separate by comma',
    	);

    You can get the Level Id by hover your mouse to the level name, please check my screenshot.
    About the XProfile Group Id, you can get it by edit the group and check the url, where you can see the param "group_id", please check my screenshot.

    If you have any issues please don't hesitate to let us know so we can assist

    Best regards,
    Hoang

  • Lyndon
    • Site Builder, Child of Zeus

    Hi,

    I added the following to my child theme's functions.php and still everyone can see the xprofile group and data :slight_frown:

    add_filter( 'bp_after_has_profile_parse_args', 'hide_profile_group' );
    function hide_profile_group( $args ) {
            $exclude_rules  = array(
    '1' => '1,2,3',
    '2' => '1,2,3',
    '3' => '1,2,3',
            );
            $exclude_groups = '';
            foreach ( $exclude_rules as $key => $val ) {
                    if ( current_user_on_level( $key ) ) {
                            $exclude_groups = $val;
                            break;
                    }
            }
            if(!empty($exclude_groups)){
                    $args['exclude_groups']=$exclude_groups;
            }
            return $args;
    }

    The code sadly did nothing :slight_frown:

  • Lyndon
    • Site Builder, Child of Zeus

    OK - A little frustrating as the problem now appears to be in the WPMU premium membership plugin. Essentially it doesn't work for visitors...

    I have the membership module set up so that registered users are treated as 'client level' - but that doesn't work for your code :slight_frown: If I manually set the user to 'client' level then it works - see my attachments for the difference.

    How can we make it work for either the 'default' client level (i.e. not manually set) or for wordpress role would be even better - much better in fact.

    Any chance, please...

  • Tyler Postle
    • CGO

    Hey Lyndon,

    Thanks for posting back. Seems like we are making progress here! @Hoang Ngo is currently offline; however, I'll let him know his expertise are still needed here when he comes back online :slight_smile:

    PS - I have no idea how the points system on here works - don't remember seeing a description when I signed up. So I 'award' points to those that help through support?

    Points are earned through posting and also helping others around the site. If you save them up long enough you can cash them in for a one year or even a lifetime free membership :slight_smile:

    Don't worry about giving us staff any points, we're here to help no matter what :slight_smile: points or no points.

    Here is the page about the points system: https://premium.wpmudev.org/earn-your-wpmudev-membership/

    Cheers,
    Tyler

  • Hoang Ngo
    • Code Slayer

    Hi @Lyndon,

    I hope you are well today and I'm sorry for the delay.

    Could you please enable support staff login in WPMU DEV dashboard > support > support access > grant access. That's way I can check your settings and have a closer look for this issue.

    Best regards,
    Hoang

  • Hoang Ngo
    • Code Slayer

    @Lyndon,

    @Hoang Ngo - Is there no way you can simple make the code you wrote check for wordpress role rather than membership level?

    Hmm, the default user role is subscriber, so I think we should follow the level, which is easier to check.

    Best regards,
    Hoang

  • Lyndon
    • Site Builder, Child of Zeus

    Yes -- That's right. I have custom roles for each user type and if your code could work with the 'default' wordpress role field that would be excellent.

    So subscriber is someone who registered
    Client role I will manually assign to user account when they spend money with me
    Expert role I manually assign when new expert joins our organisation

    I want to hide an xprofile group from everyone except those with role=expert

    I think that working with wordpress roles is best solution.

    Really appreciate you help.

  • Lyndon
    • Site Builder, Child of Zeus

    Hi @Hoang Ngo

    Yes -- That's right. I have custom roles for each user type and if your code could work with the 'default' wordpress role field that would be excellent.

    So subscriber is someone who registered
    Client role I will manually assign to user account when they spend money with me
    Expert role I manually assign when new expert joins our organisation

    I want to hide an xprofile group from everyone except those with role=expert

    I think that working with wordpress roles is best solution.

    Really appreciate you help.

  • Lyndon
    • Site Builder, Child of Zeus

    @Hoang Ngo

    I re-read your question and I want to be clear for you. There are 4 levels really.

    1) Visitor to site who does not register/login
    2) subscriber (someone registered and logged in)
    3) client - like 2 but I have manually changed their role to 'client'
    4) expert - also like 2 but again I have manually changed their role to 'expert'

    Thanks

  • Hoang Ngo
    • Code Slayer

    Hi @Lyndon,

    Many thanks for your information.

    Can you please send in:

    - Mark to my attention - ATTN: Hoang Ngo
    - Link back to this thread
    - Include admin/network access
    - Include cPanel (I will need to look at the DB so need PHPMyAdmin or similar)
    - Include FTP
    - Include any relevant URLS for your site

    On the contact form, select "I have a different question", this ensures it comes through and gets assigned to me.

    So I can check the role id and we moving to use role for this feature

    Best regards,
    Hoang

  • Lyndon
    • Site Builder, Child of Zeus

    Hi @Hoang Ngo

    I have send the information that you have requested using the form as specified. Please let me know if there's anything else you need.

    ps - functions file in child theme looks like as below;

    function buddyboss_child_setup()
    {
    load_theme_textdomain( 'buddyboss', get_stylesheet_directory() . '/languages' );
    }
    add_action( 'after_setup_theme', 'buddyboss_child_setup' );
    function buddyboss_child_scripts_styles()
    {
    wp_enqueue_style( 'buddyboss-child-custom', get_stylesheet_directory_uri().'/css/custom.css' );
    }
    add_action( 'wp_enqueue_scripts', 'buddyboss_child_scripts_styles', 9999 );
    
    /****************************** CUSTOM FUNCTIONS ******************************/
    
    // Add your own custom functions here
    
    add_action('load-users.php',function() {
    
    if(isset($_GET['action']) && isset($_GET['bp_gid']) && isset($_GET['users'])) {
        $group_id = $_GET['bp_gid'];
        $users = $_GET['users'];
        foreach ($users as $user_id) {
            groups_join_group( $group_id, $user_id );
        }
    }
        //Add some Javascript to handle the form submission
        add_action('admin_footer',function(){ ?>
        <script>
            jQuery("select[name='action']").append(jQuery('<option value="groupadd">Add to BP Group</option>'));
            jQuery("#doaction").click(function(e){
                if(jQuery("select[name='action'] :selected").val()=="groupadd") { e.preventDefault();
                    gid=prompt("Please enter a BuddyPres Group ID","1");
                    jQuery(".wrap form").append('<input type="hidden" name="bp_gid" value="'+gid+'" />').submit();
                }
            });
        </script>
        <?php
        });
    });
    // THIS REMOVES ADMIN USER FROM MEMBERS LOOP
    
    function get_user_ids_by_role($role){
            $users=array();
            $founded_users = get_users( array( 'role' => $role ) );
            if(!empty($founded_users)){
                    foreach((array)$founded_users as $user)
                            $users[]=$user->ID;
            }
    
            return $users;
    }
    
    function exclude_by_role( $exclude_roles, $implode = true ) {
        $memberArray = array();
        foreach ( $exclude_roles as $exclude_role ) {
            $memberArray = array_merge($memberArray, get_user_ids_by_role($exclude_role) );
        }
    
        if( !$implode )
            return $memberArray;
    
        $theExcludeString = implode( ",", $memberArray );
        return $theExcludeString;
    }
    
    add_action('bp_ajax_querystring','bp_exclude_byroles',20, 2);
    function bp_exclude_byroles($qs=false,$object=false){
    
            if($object!='members') //hide for members only
            return $qs;
    
            $args = wp_parse_args($qs);
            //check if we are searching for friends list etc?, do not exclude in this case
            if(!empty($args['user_id']))
              return $qs;
    
            $excluded_roles = array( 'administrator' ); // you can add roles here
            $exclude = exclude_by_role( $excluded_roles );
    
            if(!empty($args['exclude']))
              $args['exclude']=$args['exclude'].','.$exclude;
            else
              $args['exclude'] = $exclude;
    
            $qs = build_query($args);
            return $qs;
    }
    function my_setup_nav() {
          global $bp;
    
          bp_core_new_nav_item( array(
                'name' => __( 'Engagements', 'buddypress' ),
                'slug' => 'engagements',
                'position' => 120,
                'screen_function' => 'my_item_two_template'
          ) );
    
    }
    
    add_action( 'bp_setup_nav', 'my_setup_nav' );
    
    // Load a page template for your custom item. You'll need to have an item-one-template.php and item-two-template.php in your theme root.
    
    function my_item_two_template() {
          bp_core_load_template( 'engagements' );
    }
    
    //TO HIDE DAY RATES INFO FROM NON EXPERT MEMBERS//
    
    add_filter( 'bp_after_has_profile_parse_args', 'hide_profile_group' );
    function hide_profile_group( $args ) {
            $exclude_rules  = array(
    '1' => '3',
            );
            $exclude_groups = '';
            foreach ( $exclude_rules as $key => $val ) {
                    if ( current_user_on_level( $key ) ) {
                            $exclude_groups = $val;
                            break;
                    }
            }
            if(!empty($exclude_groups)){
                    $args['exclude_groups']=$exclude_groups;
            }
            return $args;
    }
  • Hoang Ngo
    • Code Slayer

    Hi there

    I have updated this code to your child theme

    add_filter( 'bp_after_has_profile_parse_args', 'hide_profile_group' );
    function hide_profile_group( $args ) {
      $exclude_groups = '';
    	if ( ! is_user_logged_in() ) {
    		//we prevent all for non login user
    		$exclude_groups = '1,2,3';
    	} else {
    		$user_info = get_userdata( get_current_user_id() );
    		$role      = reset( $user_info->roles );
    		if ( ! empty( $role ) ) {
    			$exclude_rules = array(
    				'subscriber' => '3',
    				'client'     => '3',
    				//'expert'     => ''
    			);
    			if(in_array($role,array_keys($exclude_rules))){
    				$exclude_groups = $exclude_rules[$role];
    			}
    		}
    
    	}
    
    	if ( ! empty( $exclude_groups ) ) {
    		$args['exclude_groups'] = $exclude_groups;
    	}
    
    	return $args;
    }

    Please update the group id in
    $exclude_groups = '1,2,3';
    and

    $exclude_rules = array(
    				'subscriber' => '3',
    				'client'     => '3',
    				//'expert'     => ''
    			);

    To fit with your need.

    Please give it a try and let see if it help :slight_smile:

    Best regards,
    Hoang

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.