You will need to make use of hooks and the strlen() function.
For example, at registration, in order to validate the username so that it isn’t longer than 16 characters, you can do this:
function my_username_validation() {
// Perform validation here using the strlen() function to check the field length.
}
add_action( 'bp_signup_validate', 'my_username_validation' );
Here an example for profile field lenght with some comments.
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' );
Ticket #5741
Thank you @Henry Wright and @danbp, these snippets are very useful. There is one more question about the validations, what if I want to stop the process right the way and show an error message accordingly to the user when the length of field exceeds the max length specified. Is it possible to do that inside the code snippets above?
Yes, I think you can. Try this:
function my_username_validation() {
if ( strlen( $_POST['signup_username'] ) > 16 ) {
global $bp;
$bp->signup->errors['signup_username'] = __( 'Put your error message here.', 'buddypress' );
}
}
add_action( 'bp_signup_validate', 'my_username_validation' );