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

(string) (Required)

$value

(string) (Required)

$user_id

(string) (Optional)

Default value: null

Return

($retval)

Source

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

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.