bp_xprofile_validate_nickname_value( $retval,  $field_id, string $value, string $user_id = null )

Validate nickname approved characters and format.

Description

Parameters

$retval

(Required)

$field_name

(Required)

$value

(Required)

$user_id

(Optional)

Default value: null

Return

($retval)

Source

File: bp-xprofile/bp-xprofile-filters.php

760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
function bp_xprofile_validate_nickname_value( $retval, $field_id, $value, $user_id = null ) {
    if ( $field_id != bp_xprofile_nickname_field_id() ) {
        return $retval;
    }
 
    if ( $retval ) {
        return $retval;
    }
 
    $value = strtolower( $value );
    $field_name = xprofile_get_field( $field_id )->name;
 
    // Empty nickname
    if ( '' === trim( $value ) ) {
        return sprintf( __( '%s is required and not allowed to be empty.', 'buddyboss' ), $field_name );
    }
 
    // only alpha numeric, underscore, dash
    if ( ! preg_match( '/^([A-Za-z0-9-_\.]+)$/', $value ) ) {
        return sprintf( __( 'Invalid %s. Only "a-z", "0-9", "-", "_" and "." are allowed.', 'buddyboss' ), $field_name );
    }
 
    // cannot have 2 continued special characters
    if ( preg_match( '/([-_\.]{2})/', $value ) ) {
        return sprintf( __( '"-", "_" and "." cannot be repeated twice in %s.', 'buddyboss' ), $field_name );
    }
 
    // must be shorter then 32 characters
    $nickname_length = apply_filters( 'xprofile_nickname_max_length', 32 );
    if ( strlen( $value ) > $nickname_length ) {
        return sprintf( __( '%s must be shorter than %d characters.', 'buddyboss' ), $field_name, $nickname_length );
    }
 
    global $wpdb;
    $where = [
        'meta_key = "nickname"',
        'meta_value = "' . $value . '"'
    ];
 
    if ( $user_id ) {
        $where[] = 'user_id != ' . $user_id;
    }
 
    $sql = sprintf(
        'SELECT count(*) FROM %s WHERE %s',
        $wpdb->usermeta,
        implode( ' AND ', $where )
    );
 
    if ( $asdf = $wpdb->get_var( $sql ) > 0 ) {
        return sprintf( __( '%s has already been taken.', 'buddyboss' ), $field_name );
    }
 
    return $retval;
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.