Skip to:
Content
Pages
Categories
Search
Top
Bottom

Conditional Exclusion of a xProfile Fields Group from editing


  • Iurie Malai
    Participant

    @flegmatiq

    Hi!

    I have created in the Base (Primary) extended profile group a drop-down menu/field ‘User Type’ with three options (Faculty, Student, Visitor). Also, I have created an extended profile fields group ‘Academic Profile’ (id number 2).

    What I want to do is to hide the ‘Academic Profile’ fields group from editing (not displaying on the user edit profile page) if the drop-down field ‘User Type’ from the Base group is empty or not ‘Faculty’.

    I have the next code in the bp-custom.php, but it not works. What am I doing wrong?

    /* xProfile display for faculties */
    function exclude_xProfile_group_if_user_is_not_a_faculty( $args = array() ) {
    
       //Check if the user is not a faculty
       if ( xprofile_get_field_data( 'User Type', $user_ID ) != 'Faculty' ) :
          //Comma-separated list of xProfile fields group IDs to exclude
          $args['exclude_groups'] = "2";
       endif; 
    
       return $args;
    }
    
    add_filter( 'has_profile', 'exclude_xProfile_group_if_user_is_not_a_faculty');
    

    WordPress 3.9.1
    Buddypress 2.0.1
    http://upsic.org/

Viewing 7 replies - 1 through 7 (of 7 total)

  • danbp
    Participant

    @danbp

    Since BP 2.0, you can use bp_parse_args to customise template loops and on a much easier way as never before.

    The example function below will hide profile groups with ID 1 and profile fields IDs 6, 18 & 39 on the public profile screen of each member and stay visible on the edit screen. Tested & approved 2.0.1

    If you get some php warning while using this function, be aware that there is a bug in 2.0.1 bp-xprofile-classes.php (fixed. See ticket). Simply apply the lattest patch to get rid of this error.

    function flegmatiq_profiles( $retval ) {	
       // conditionnals to set accordingly to your purpose
    	if( !is_user_logged_in()  && !bp_is_profile_edit() ) { 
    		$retval['exclude_groups'] = '2';	
    		$retval['exclude_fields'] = '6,18,39';	
    		
    	} 
    	return $retval;
    	
    }
    add_filter( 'bp_after_has_profile_parse_args', 'flegmatiq_profiles' );

    In your particular case, you have to play with the conditionnals

    The example below is to give you an idea how to handle this. Not tested !

    function flegmatiq_profiles( $retval ) {
    $myfield = xprofile_get_field_data( 'Faculty', $user_id );
    if( empty( $myfield ) )
    return; // or echo
    
       if( $myfield == "Faculty" ) 
          if( bp_is_profile_edit() ) { 
    		$retval['exclude_groups'] = '1,56';	
    		$retval['exclude_fields'] = '6,18,39';	.
          }
       if $myfield == "field_name_2";
         // do something other...
    
    return $myfield;
    
    add_filter( 'bp_after_has_profile_parse_args', 'flegmatiq_profiles' );

    Iurie Malai
    Participant

    @flegmatiq

    @danbp, thank you! I tested your solution (the last function), but no success. Is enough to put this in the bp-custom.php file?

    I tried the next simple function to hide the extended fields group number 2 from editing for all members, but it not working.

    
    function flegmatiq_profiles( $retval ) {	
    	
    	if(  bp_is_profile_edit() ) {		
    		$retval['exclude_groups'] = '2';
    	}	
    	
    	return $retval;
    	
    }
    add_filter( 'bp_after_has_profile_parse_args', 'flegmatiq_profiles' );

    danbp
    Participant

    @danbp

    I use this function and it works.

    Perhaps try to modify the field view of group id 2,in the profile setting. View for members only and set them also so that members cannot change the view.

    Have you also applied the patch given in the ticket ?


    joesegal
    Participant

    @joesegal

    Sorry to drop in on your thread but I was hoping you all might be able to recommend a buddypress developer to me? Please let me know if you can. Thanks!, Joe


    Iurie Malai
    Participant

    @flegmatiq

    @danbp, I applied the patch but the function does not works. If I put your function in the bp-custom.php I have a clean white page (display) as a result.


    danbp
    Participant

    @danbp

    Generally after adding some custom code and getting a blank page afterward is due to a PHP error. So before saying it’s not working (and it is), you have to check attentively what you did. A missing semi-colon, apostrophe, etc….


    Iurie Malai
    Participant

    @flegmatiq

    @danbp, I know how to check for errors, but this didn’t helped me too much :). I solved with the blank page, but no more. Your solution works for hiding some xProfile tabs from viewing, but not from editing them. Am I wrong? Sorry!

    This is my tested function (as I said, it hiding only from viewing, not from editing):

    
    function flegmatiq_profiles( $retval ) {
    
       if ( is_user_logged_in() && !bp_is_profile_edit() ) {
          $myfield = xprofile_get_field_data( 'User Type' );
          if( $myfield == "Faculty" ) {
             $retval['exclude_groups'] = '2';
    	 //$retval['exclude_fields'] = '6,18,39';
          }
       }
    
       return $retval;
    
    }
    
    add_filter( 'bp_after_has_profile_parse_args', 'flegmatiq_profiles' );
    

    The next code works as I need, but I think it is not so elegant, and it not allow to use the group ID, only his order number, as ordered by admin:

    
    function hide_profile_group_tabs_from_editing( $group_names ){
    
       if ( is_user_logged_in() && bp_is_profile_edit() && xprofile_get_field_data( "User Type") != "Faculty" ) {
          for ($x=0; $x<=sizeof($group_names); $x++) {
             if ( $x != 3 ) echo $group_names[$x]; //3 is the order number (from 0) of the tab that I need to hide in the profile edit page. This is not the group ID.
          }
       } else {
          return $group_names;
       }
    
    }
    
    add_filter('xprofile_filter_profile_group_tabs', 'hide_profile_group_tabs_from_editing');
    
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Conditional Exclusion of a xProfile Fields Group from editing’ is closed to new replies.
Skip to toolbar