Replies
/** * 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; } );
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. 🙁