BuddyDev

Search

Xprofile Character Limit

  • Participant
    Level: Initiated
    Posts: 11
    Joy on #51832

    Thank you! How do I adjust to target group 2, field 3 please?

  • Participant
    Level: Initiated
    Posts: 11
    Joy on #51833

    I tested it by just using the field id 3 as in your example. However it doesn’t seem to work. When this snippet is activated, the field won’t even update. It “saves” and refreshes, but the content remains unchanged whether I add or remove the text. Also when I add more than the character limit, I don’t get any notification of a problem either. 🙁

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2985
    Ravi on #51841

    Hello Joy,

    Thank you for the acknowledgement. Please let me know the following things:

    1. On which screen you have checked this i.e. on registration or edit profile page.
    2. If you are checking on the edit profile screen are you modifying someone else profile?

    Please share your modified version of the code so that I can check.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 11
    Joy on #51850

    Hi Ravi,

    Thank you for your response.

    1. I am trying to change the field on the edit profile page.
    2. I am trying to modify my own profile to test the functionality.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2985
    Ravi on #52095

    Hello Joy,

    Thank you for the acknowledgement. Can you share the code you are using so that I can check?

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 11
    Joy on #52103
    /**
     * Retrieves words threshold value based on field id.
     *
     * @param int $field_id Field id.
     *
     * @return int|null
     */
    function buddydev_get_words_threshold_value( int $field_id ) {
    
    	// Field id with no. of words limit. Replace with yours.
    	$field_thresholds = array(
    	//	1 => 10,
    	//	2 => 20,
    		3 => 30,
    	);
    
    	return isset( $field_thresholds[ $field_id ] ) ? $field_thresholds[ $field_id ] : null;
    }
    
    /**
     * Checks if field's data reached their words limit or not.
     *
     * @param int $field_id      Field id.
     * @param string $field_data Field data.
     *
     * @return bool
     */
    function buddydev_has_reached_words_threshold_value( int $field_id, string $field_data, int $field_threshold_value ) {
    
    	if ( strlen( trim( $field_data ) ) > $field_threshold_value ) {
    		return true;
    	}
    
    	return false;
    }
    
    add_action( 'bp_signup_validate', function () {
    	if ( ! bp_is_post_request() || ! bp_is_active( 'xprofile' ) || empty( $_POST['signup_profile_field_ids'] ) ) {
    		return;
    	}
    
    	$profile_field_ids = explode( ',', $_POST['signup_profile_field_ids'] );
    
    	foreach ( (array) $profile_field_ids as $field_id ) {
    
    		if ( empty( $_POST[ 'field_' . $field_id ] ) ) {
    			continue;
    		}
    
    		$threshold_value = buddydev_get_words_threshold_value( $field_id );
    		$field_data      = trim( $_POST[ 'field_' . $field_id ] );
    
    		if ( $threshold_value && buddydev_has_reached_words_threshold_value( $field_id, $field_data, $threshold_value ) ) {
    			buddypress()->signup->errors[ 'field_' . $field_id ] = sprintf( 'Number of words limit exceeded then field allowed limit which is %d words.', $threshold_value );
    		}
    	}
    } );
    
    add_action( 'bp_actions', function () {
    
    	if ( ! bp_is_my_profile() || ! bp_is_profile_component() ) {
    		return;
    	}
    
    	// Make sure a group is set.
    	if ( ! bp_action_variable( 1 ) || ! isset( $_POST['field_ids'] ) ) {
    		return;
    	}
    
    	// Check the field group exists.
    	if ( ! bp_is_action_variable( 'group' ) || ! xprofile_get_field_group( bp_action_variable( 1 ) ) ) {
    		return;
    	}
    
    	check_admin_referer( 'bp_xprofile_edit' );
    
    	// No errors.
    	$path_chunks = array( bp_get_profile_slug(), 'edit' );
    	$field_ids   = wp_parse_id_list( $_POST['field_ids'] );
    
    	// Check to see if any new information has been submitted.
    	$error = false;
    	foreach ( $field_ids as $field_id ) {
    
    		if ( empty( $_POST[ 'field_' . $field_id ] ) ) {
    			continue;
    		}
    
    		$threshold_value = buddydev_get_words_threshold_value( $field_id );
    		$field_data      = trim( $_POST[ 'field_' . $field_id ] );
    
    		if ( $threshold_value && buddydev_has_reached_words_threshold_value( $field_id, $field_data, $threshold_value ) ) {
    			$error = true;
    			break;
    		}
    	}
    
    	if ( $error ) {
    		bp_core_add_message( __( 'Words limit exceeded. Please check!', 'buddypress' ), 'error' );
    
    		$path_chunks[] = array( 'group', bp_action_variable( 1 ) );
    		bp_core_redirect( bp_displayed_user_url( bp_members_get_path_chunks( $path_chunks ) ) );
    	}
    }, 5 );
    
    add_filter( 'bp_get_the_profile_field_description', function ( $description ) {
    	$threshold_value = buddydev_get_words_threshold_value( bp_get_the_profile_field_id() );
    
    	if ( $threshold_value ) {
    		$description .= ' ' . sprintf( 'This Field words limit is: %s', $threshold_value );
    	}
    
    	return $description;
    } );
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2985
    Ravi on #52134

    Hello Joy,

    Thank you for sharing the code. I will check it by today and will update you.

    Regards
    Ravi

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2985
    Ravi on #52135
    This reply has been marked as private.

You must be logged in to reply to this topic.

This topic is: not resolved