BuddyDev

Search

Problems following Extending BuddyPress profile field visibility

  • Participant
    Level: Initiated
    Posts: 5
    matthew flamm on #302

    After following your tutorial at https://buddydev.com/buddypress/extending-buddypress-profile-field-visibility/ I’ve run into a little problem. When the user tries to edit one of the fields hidden for other users on their own profile, it returns the error “Please make sure you fill in all required fields in this profile field group before saving.”

    the field that causes there error isn’t set to required, and it’s not empty (which seems to be the only reasons the errors gets output). I’m at a loss how this might happen and was hoping you could help point me in the right direction.

    It looks like it’s coming here in bp-xprofile-screens.php on lines 90-114

    foreach ( (array) $posted_field_ids as $field_id ) {
    if ( !isset( $_POST[‘field_’ . $field_id] ) ) {

    if ( !empty( $_POST[‘field_’ . $field_id . ‘_day’] ) && !empty( $_POST[‘field_’ . $field_id . ‘_month’] ) && !empty( $_POST[‘field_’ . $field_id . ‘_year’] ) ) {
    // Concatenate the values
    $date_value = $_POST[‘field_’ . $field_id . ‘_day’] . ‘ ‘ . $_POST[‘field_’ . $field_id . ‘_month’] . ‘ ‘ . $_POST[‘field_’ . $field_id . ‘_year’];

    // Turn the concatenated value into a timestamp
    $_POST[‘field_’ . $field_id] = date( ‘Y-m-d H:i:s’, strtotime( $date_value ) );
    }

    }

    $is_required[$field_id] = xprofile_check_is_required_field( $field_id );
    if ( $is_required[$field_id] && empty( $_POST[‘field_’ . $field_id] ) ) {
    $errors = true;
    }
    }

    // There are errors
    if ( !empty( $errors ) ) {
    bp_core_add_message( __( ‘Please make sure you fill in all required fields in this profile field group before saving.’, ‘buddypress’ ), ‘error’ );

    // No errors
    }

  • Keymaster
    (BuddyDev Team)
    Posts: 21785
    Brajesh Singh on #303

    Hi Matthew,
    Welcome to the BuddyDev forums.

    How are you hiding the field? You must exclude it from the profile loop like I have done for
    https://buddydev.com/plugins/bp-non-editable-profile-fields/

    It seems you are hiding with css? To make BuddyPress deal with the hidden fields properly( required fields), you need to exclude it from the loop when not required.

    Have a look at this filer
    https://github.com/buddydev/bp-non-editable-profile-fields/blob/master/bp-non-editable-field.php#L27

    and the attached method to see an example.

    Did that help?

    PS: Please use backticks (`) to post code in the forum.

  • Participant
    Level: Initiated
    Posts: 5
    matthew flamm on #305

    Thank you for your amazingly quick reply!

    Actually I’m hiding with the code below setup as a plugin. using the code below I get the new visibility option expected, and the field is hidden from the members I expect it to be, but when a user tries to edit the field on their own profile they get the error mentioned and nothing is saved. It only occurs when they try and edit the xprofile group with a field set to one of the new visibilities.

    while trying to figure this out I have tested:
    field > set to “groups” visibility > required > error!
    field > set to “membership2” visibility > required > error!
    field > set to “groups” visibility > not required > error!
    field > set to “membership2” visibility > not required > error!
    field > set to any core visibility > required > works
    field > set to works visibility > required > works

    I also know it is my fields because deleting them or changing them to a core visibility makes the error go away.

    I have tried it with a text box (“test” being entered as the value when editing), radio button, and dropdown.

    I have removed my addition for the membership2 visibility, still the same problem with the group visibility. I have also removed group and confirmed the problem still for membership2

    Here is the code I’m using.

    
    <?php
    
    add_filter( 'bp_xprofile_get_visibility_levels', 'buddydev_customize_profile_visibility' );
    function buddydev_customize_profile_visibility( $allowed_visibilities ) {
    	
    	//add custom visibility 
    	if( bp_is_active( 'groups' ) ) {
    	
    		$allowed_visibilities['groups'] = array(
    			'id'	=> 'groups',
    			'label'	=> _x( 'My Group Members', 'Visibility level setting', 'bp-extended-profile-visibility' )
    		);
    		
    	}
    
    	
    	//this has been tested with this section for membership 2 here and removed
    	
    	include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); //needed to use "is_plugin_active()"
    	
    	//add visibility for fields based on Membership Level, using Membership 2 Pro from WPMUDev
    	if ( is_plugin_active( 'membership/membership2.php' ) ) { //correctly checks for plugin to be active, but only if plugin.php is included above.  otherwise frontend has error of undefined function for is_plugin_active()
    		$allowed_visibilities['membership2'] = array(
    			'id'	=> 'membership2',
    			'label'	=> _x( 'Logged-in Pro Members', 'Visibility level setting', 'bp-extended-profile-visibility' )
    		);
    	}	
    	
    	return $allowed_visibilities;
    }
    
    //now do some magic
    add_filter( 'bp_xprofile_get_hidden_field_types_for_user', 'buddydev_get_hidden_visibility_types_for_user', 10, 3 );
    		
    function buddydev_get_hidden_visibility_types_for_user( $hidden_levels, $displayed_user_id, $current_user_id ) {
    	//if it is not my data and super admin is not viewing and there are no common groups, then hide
    	if( ( $displayed_user_id != $current_user_id ) &&  ! buddydev_user_has_common_group( $displayed_user_id, $current_user_id ) && ! is_super_admin() ) {
    		
    		$hidden_levels[] = 'groups'; //profile field with this privacy level will be hidden for the user
    		
    	}
    	
    	if( ( $displayed_user_id != $current_user_id ) &&  !current_user_on_level(239)  && ! is_super_admin() ) { //&&  !current_user_on_level(239)  this is used to test membership level on the membership plugin. confirmed to work in a different function.  
    		
    		//this gets set or not set correctly based on membership level, so conditional is working.
    		$hidden_levels[] = 'membership2'; //profile field with this privacy level will be hidden for the user
    		
    	}
    	
    	return $hidden_levels;
    	
    }
    
    /**
     * Check if there are any common groups between these two users
     * 
     * @param int $user_id
     * @param int $other_id
     * @return boolean true if ye, else false
     */
    function buddydev_user_has_common_group( $user_id, $other_id ) {
    	
    	$common_groups = buddydev_get_common_groups( $user_id, $other_id );
    	
    	if( ! empty( $common_groups ) )
    		return true;
    	
    	return false;
    	
    }
    /**
     * Get all the common groups between these two users
     * 
     * @global type $wpdb
     * @param int $user_id
     * @param int $other_id
     * @return mixed array of group ids 
     */
    function buddydev_get_common_groups( $user_id, $other_id ) {
    	
    	global $wpdb;
    	
    	//members table
    	$table = buddypress()->groups->table_name_members;
    	
    	$query_user_groups = $wpdb->prepare( "SELECT group_id FROM {$table} WHERE user_id = %d AND is_confirmed = %d ", $user_id, 1 );
    	$query_other_user_groups = $wpdb->prepare( "SELECT group_id FROM {$table} WHERE user_id = %d AND is_confirmed = %d ", $other_id, 1 );
    	
    //should we put a LIMIT clause too, I am not putting as someone may find this function useful for other purposes
    	$commpon_groups = $wpdb->get_col( "{$query_user_groups} AND group_id IN ({$query_other_user_groups})" );
    	
    	
    	return $commpon_groups;
    }
    
  • Keymaster
    (BuddyDev Team)
    Posts: 21785
    Brajesh Singh on #310

    Hi Matthew,
    Thank you for posting the code.
    I am sorry but I have to leave due to some urgent personal work, I will get back to you a little late( around 10 hours from now) when I am back at desk.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    matthew flamm on #312

    That’s not a problem. I’m already impressed with how quickly you’re able and willing to help.

  • Keymaster
    (BuddyDev Team)
    Posts: 21785
    Brajesh Singh on #321

    Hi Matthew,
    Thank you.
    i am looking at it now. Sorry about yesterday. Got stuck in something else.

  • Keymaster
    (BuddyDev Team)
    Posts: 21785
    Brajesh Singh on #322

    Hi Matthew,
    I have looked at it in detail now.

    1. Please do note that the profile visibility levels hides the fields for other user(displayed fields) but do not hide the fields on the edit screen as the user needs to update the data(which ma or may not be visible to a subset of users)

    I tested with the group privacy and it worked fine for me. I tried marking field as required and updated, then I marked the field as non required and updated, both went well.

    So, I have come to the conclusion that the privacy level is not the issue here.

    Are you using any other plugin that validates the field or something like that? That might be causing the issue.

  • Participant
    Level: Initiated
    Posts: 5
    matthew flamm on #413

    The only extensions I have active are akismet, buddypress, and the code above.

    Could there be something else interfering?

  • Keymaster
    (BuddyDev Team)
    Posts: 21785
    Brajesh Singh on #422

    Hi Matthew,
    I don’t think Akismet or BuddyPress can cause it.

    Do you have any code in bp-custom.php or functions.php of your theme?

    Also, Can you please confirm if the behavior is different than what I posted in my previous repl.

You must be logged in to reply to this topic.

This topic is: not resolved