-
-
Save raviousprime/89346dad536031d8d7c056219e958880 to your computer and use it in GitHub Desktop.
apply words limit to profile fields on signup form and profile edit form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 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, | |
6 => 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 ) { | |
$field = xprofile_get_field( $field_id ); | |
if ( ! $field || empty( $_POST[ 'field_' . $field_id ] ) ) { | |
continue; | |
} | |
$threshold_value = buddydev_get_words_threshold_value( $field_id ); | |
$field_data = $threshold_value ? trim( (string) $_POST[ 'field_' . $field_id ] ) : ''; | |
if ( $field_data && 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_post_request() || ! 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. | |
$field_ids = wp_parse_id_list( $_POST['field_ids'] ); | |
// Check to see if any new information has been submitted. | |
$error = false; | |
$field_names = array(); | |
foreach ( $field_ids as $field_id ) { | |
$field = xprofile_get_field( $field_id ); | |
if ( ! $field || empty( $_POST[ 'field_' . $field_id ] ) ) { | |
continue; | |
} | |
$threshold_value = buddydev_get_words_threshold_value( $field_id ); | |
$field_data = $threshold_value ? trim( (string) $_POST[ 'field_' . $field_id ] ) : ''; | |
if ( $field_data && buddydev_has_reached_words_threshold_value( $field_id, $field_data, $threshold_value ) ) { | |
$error = true; | |
$field_names[] = apply_filters( 'bp_get_the_profile_field_name', $field->name ); | |
} | |
} | |
if ( $error ) { | |
$field_names = join( ',', $field_names ); | |
$message = sprintf( 'Words limit exceeded for %s.', $field_names ); | |
bp_core_add_message( $message, 'error' ); | |
if ( function_exists( 'bp_displayed_user_url' ) ) { | |
$path_chunks = array( bp_get_profile_slug(), 'edit' ); | |
$path_chunks[] = array( 'group', bp_action_variable( 1 ) ); | |
$redirect_url = bp_displayed_user_url( bp_members_get_path_chunks( $path_chunks ) ); | |
} else { | |
$redirect_url = bp_core_get_user_domain( bp_displayed_user_id() ); | |
$profile_slug = bp_get_profile_slug(); | |
$group_id = bp_action_variable( 1 ); | |
$redirect_url = trailingslashit( $redirect_url . "{$profile_slug}/edit/group/{$group_id}/" ); | |
} | |
bp_core_redirect( $redirect_url ); | |
} | |
}, 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; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment