ulgm_add_new_user_data
Filters the data array used for adding a new user, allowing modification before user creation.
add_filter( 'ulgm_add_new_user_data', $callback, 10, 1 );
Description
Allows developers to modify user data before a new user is created in the system. Use this filter to add custom fields, change existing values, or perform validation. The hook receives the user data array, and you should return the modified array.
Usage
add_filter( 'ulgm_add_new_user_data', 'your_function_name', 10, 1 );
Parameters
-
$user_data(mixed) - This parameter contains an array of data intended for creating a new user, including their login, email, and potentially other user-related information.
Return Value
The filtered value.
Examples
/**
* Example filter to modify user data before user creation.
*
* This example adds a default user role to new users if no role is specified.
*
* @param array $user_data The array of user data being prepared for creation.
* @return array The modified user data array.
*/
add_filter( 'ulgm_add_new_user_data', function( $user_data ) {
// Check if a user role is already set.
if ( ! isset( $user_data['role'] ) || empty( $user_data['role'] ) ) {
// Set a default role if none is provided.
$user_data['role'] = 'subscriber';
}
// You could also add other modifications here, for example:
// If the user's first name is empty, set it to their username.
// if ( empty( $user_data['first_name'] ) && isset( $user_data['user_login'] ) ) {
// $user_data['first_name'] = $user_data['user_login'];
// }
return $user_data;
}, 10, 1 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/group-management/class-group-management-helpers.php:251
// If email exists and username is blank: Make username = email.
if ( $username_column_exists && isset( $user_data['username'] ) && empty( $user_data['username'] ) ) {
$user_data['user_login'] = $user_data['user_email'];
}
// Create the user.
$user_data = apply_filters( 'ulgm_add_new_user_data', $user_data );
if ( is_array( $user_data ) && array_key_exists( 'error-code', $user_data ) ) {
if ( ! $counter ) {
$data['message'] = $user_data['error-code'];
if ( $is_api ) {
wp_send_json_error( $data );
}
} else {