Home / eCommerce / Adds a username field to WC Vendors Signup and uses it when creating the user.
Duplicate Snippet

Embed Snippet on Your Site

Adds a username field to WC Vendors Signup and uses it when creating the user.

This feature adds a Username field to the WC Vendors signup form, allowing vendors to choose their preferred username during registration. When the form is submitted, the selected username will be used to create the vendor’s WordPress user account instead of generating one automatically. This helps maintain consistency and gives vendors more control over their account details.

Code Preview
php
<?php
/**
 * Username Field
 * Description: Adds a username field to WC Vendors Signup and uses it when creating the user.
 */
defined( 'ABSPATH' ) || exit;
add_filter( 'wcv_signup_form_config', function( $config ) {
	$config = is_array( $config ) ? $config : array();
	$has_core_username = array_key_exists( 'username', $config );
	$has_custom_username = array_key_exists( 'wcv_cf_username', $config );
	// If both exist somehow, keep the admin/custom one and remove the core one to avoid duplicates.
	if ( $has_core_username && $has_custom_username ) {
		unset( $config['username'] );
		$has_core_username = false;
	}
	// If neither exists, add a custom (builder-manageable) username field with sane defaults.
	if ( ! $has_core_username && ! $has_custom_username ) {
		$config['wcv_cf_username'] = array(
			'type'        => 'text',
			'label'       => __( 'Username', 'wc-vendors-signup' ),
			'required'    => true,   // default; admin toggle can change later
			'deletable'   => false,  // prevent accidental removal if you prefer
			'editable'    => true,
			'order'       => 1,      // default position; we do not reorder other fields
			'enabled'     => true,   // default; admin toggle can change later
			'placeholder' => __( 'Choose a username', 'wc-vendors-signup' ),
			'is_default'  => false,  // it’s a custom field, not a built-in default
			'show_on_checkout' => false,
		);
	}
	return $config;
}, 5 );
function wcv_signup_get_username_field_key() {
	$config = apply_filters( 'wcv_signup_form_config', get_option( 'wcv_signup_form_config', array() ) );
	// Prefer custom if present.
	if ( isset( $config['wcv_cf_username'] ) && ! empty( $config['wcv_cf_username']['enabled'] ) ) {
		return 'wcv_cf_username';
	}
	// Fallback to legacy/core if present (we also cleaned this up above, but keep fallback just in case).
	if ( isset( $config['username'] ) && ! empty( $config['username']['enabled'] ) ) {
		return 'username';
	}
	return null;
}
add_filter( 'wcv_signup_validation_errors', function( $errors, $form_data ) {
	$username_key = wcv_signup_get_username_field_key();
	if ( ! $username_key ) {
		return $errors;
	}
	// Get the live field config after filters so toggles are respected.
	$config = apply_filters( 'wcv_signup_form_config', get_option( 'wcv_signup_form_config', array() ) );
	$field  = $config[ $username_key ];
	$is_enabled  = ! empty( $field['enabled'] );
	$is_required = ! empty( $field['required'] );
	$label       = isset( $field['label'] ) ? (string) $field['label'] : __( 'Username', 'wc-vendors-signup' );
	$raw_value   = isset( $form_data[ $username_key ] ) ? (string) $form_data[ $username_key ] : '';
	$value       = sanitize_user( $raw_value, true );
	if ( ! $is_enabled ) {
		return $errors; // field disabled; skip validation
	}
	if ( $is_required && $value === '' ) {
		$errors[ $username_key ] = array(
			'message' => __( 'Please enter a username.', 'wc-vendors-signup' ),
			'label'   => $label,
		);
		return $errors;
	}
	// If provided (required or optional), validate format and uniqueness.
	if ( $value !== '' ) {
		if ( strlen( $value ) < 3 ) {
			$errors[ $username_key ] = array(
				'message' => __( 'Username must be at least 3 characters.', 'wc-vendors-signup' ),
				'label'   => $label,
			);
			return $errors;
		}
		if ( $value !== sanitize_user( $value, true ) ) {
			$errors[ $username_key ] = array(
				'message' => __( 'Username contains invalid characters.', 'wc-vendors-signup' ),
				'label'   => $label,
			);
			return $errors;
		}
		if ( username_exists( $value ) ) {
			$errors[ $username_key ] = array(
				'message' => __( 'This username is already taken.', 'wc-vendors-signup' ),
				'label'   => $label,
			);
			return $errors;
		}
		$GLOBALS['wcv_signup_requested_username'] = $value; // store for step 3
	}
	return $errors;
}, 10, 2 );
add_filter( 'pre_user_login', function( $user_login ) {
	$is_wcv_signup = ( isset( $_POST['action'] ) && 'wcv_process_registration' === $_POST['action'] ) // phpcs:ignore
		|| isset( $_POST['wcv_registration_nonce'] ) // phpcs:ignore
		|| isset( $_POST['nonce'] ); // phpcs:ignore
	if ( ! $is_wcv_signup ) {
		return $user_login;
	}
	// Prefer the submitted value by the actual, active field key.
	$username_key = wcv_signup_get_username_field_key();
	if ( $username_key && isset( $_POST[ $username_key ] ) ) { // phpcs:ignore
		$requested = sanitize_user( (string) $_POST[ $username_key ], true ); // phpcs:ignore
		if ( $requested && ! username_exists( $requested ) ) {
			return $requested;
		}
	}
	// Fallback to the validated/stashed value if present.
	if ( ! empty( $GLOBALS['wcv_signup_requested_username'] ) ) {
		$requested = sanitize_user( (string) $GLOBALS['wcv_signup_requested_username'], true );
		if ( $requested && ! username_exists( $requested ) ) {
			return $requested;
		}
	}
	return $user_login;
}, 10 );

Comments

Add a Comment