Skip to:
Content
Pages
Categories
Search
Top
Bottom

limit the length of xprofile fields to a specific amount of characters

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

  • kobrakai75
    Participant

    @kobrakai75

    @snorlax775

    Did you find a solution for this?

    I’m also trying to find a way to limit characters for profile fields.


    rsmithgs
    Participant

    @rsmithgs

    The link in your previously linked solution is broken, but archive.org shows this from March 2, 2017 (https://web.archive.org/web/20170302040249/http://snippetbarn.bp-fr.net/xprofile-field-length-control)

    Google Translated to English:

    Imagine that you have a profile field containing any biography or presentation and want to display it, at least as an excerpt, on the membership directory.
    Let’s imagine you have an xprofile containing a biography that you want to display, even partially, on the members directory.

    function bpfr_custom_textfield_length() {  
         
        //Check if user is logged in & if xprofile component is activated
        if ( is_user_logged_in() && bp_is_active( 'xprofile' ) ) : 
            $my_custom_textfield = bp_get_member_profile_data( 'field=Brief Biography&user_id='.bp_get_member_user_id() ); 
         
        /*
         * The length = number of characters, not words.
         * Set the number of caracters to show.
         * The 3 dots are the appended text ending the excerpt.
         * Don't remove the quotes if you change this
         * BuddyPress 2.1 will add new class and id for custom fields.
         * The span can be omited to style this part. See ticket #5741
         */
            if ( strlen($my_custom_textfield) > 20) :  //adjust to your need
                $my_custom_textfield = substr($my_custom_textfield, 20).'...'; //adjust to your need
            endif;
                // uncomment the following line to get a span around the displayed field content
            // echo '<span class="short-custom">'. $my_custom_textfield; .'</span>';
                // comment the following line if you use the span
            echo $my_custom_textfield;
                 
     
        endif; // is_user_logged_in
    }
    add_action( 'bp_directory_members_item', 'bpfr_custom_textfield_length' );

    I have not tested this code, just reporting what was originally said to resolve this issue. I am looking to find out if this works as well.


    kobrakai75
    Participant

    @kobrakai75

    @rsmithgs

    Thanks a lot for this.

    I’ll test it out when I get a chance and let you know the result.


    kobrakai75
    Participant

    @kobrakai75

    @rsmithgs

    You can try the following to limit character length in xprofile fields:

    function buddydev_limit_xpfield_length( $validated, $values, $field ) {
    	$allowed_len = 0;
    
    	if ( $field instanceof BP_XProfile_Field_Type_Textarea ) {
    		$allowed_len = 600;
    	} elseif ( $field instanceof BP_XProfile_Field_Type_Textbox ) {
    		$allowed_len = 60; 
    	}
    
    	if ( $allowed_len ) {
    		$validated = strlen( $values ) < $allowed_len;
    	}
    
    	return $validated;
    }
    
    add_filter( 'bp_xprofile_field_type_is_valid', 'buddydev_limit_xpfield_length', 10, 3 );

    It worked for me!


    rsmithgs
    Participant

    @rsmithgs

    @kobrakai75 thank you so much for this!

    Just asking about this one piece.

    $allowed_len = 0 isn’t a problem because the if loop will only run if $allowed_len is anything other than 0 right? if($allowed_len) would be if(0) which translates to if(false) i.e. (don’t run), right?


    rsmithgs
    Participant

    @rsmithgs

    @kobrakai75
    It seems to have worked, but is there a way to have a more clear error message? I noticed that it worked for notifying the user that there was an error, but it doesn’t specify anything and it leaves the field that had the problem blank. If they just wrote their whole life story in a Textarea field this could be very heart shattering that all of their work is now gone instead of just specifying that it was a specific field and then saying “this field has too many characters”.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar