Filter uncanny-learndash-groups

ulgm_bulk_add_new_user_validation

Filters the data during bulk user creation, allowing modification of validation checks before new users are added.

add_filter( 'ulgm_bulk_add_new_user_validation', $callback, 10, 2 );

Description

Filters user data during bulk user addition for validation. Developers can use this hook to add custom validation rules or modify error messages. It fires before a user is processed for bulk addition, allowing modification of the `$validation_errors` array.


Usage

add_filter( 'ulgm_bulk_add_new_user_validation', 'your_function_name', 10, 2 );

Parameters

$user_data (mixed)
This parameter is an array containing the data for the user being added, including email, ID, first name, last name, and role.
$k (mixed)
This parameter is a string representing the specific action being performed, which is 'bulk_add_users' in this context.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the 'ulgm_bulk_add_new_user_validation' filter hook.
 * This example adds a validation to ensure that all email addresses are in a specific domain.
 *
 * @param array $validation_errors An array of existing validation errors.
 * @param array $user_data         An array containing the data for the user being processed.
 * @param string $context          The context of the filter, which is 'bulk_add_users' in this case.
 * @param int $k                   The index or key of the current user being processed in the bulk operation.
 * @return array                   The updated array of validation errors.
 */
function my_custom_bulk_user_email_domain_validation( $validation_errors, $user_data, $context, $k ) {
    // Only apply this validation if the context is correct and we have user data.
    if ( 'bulk_add_users' === $context && isset( $user_data['user_email'] ) ) {
        $email = $user_data['user_email'];
        $allowed_domain = '@example.com'; // Change this to your desired domain

        // Check if the email address belongs to the allowed domain.
        if ( strpos( $email, $allowed_domain ) === false ) {
            // Add a new error to the validation_errors array.
            // The key of the error can be used to identify which user had the error,
            // and the value is the error message.
            $validation_errors["user_{$k}_email_domain"] = sprintf(
                __( 'User %d: Email address "%s" is not allowed. Only emails from %s are permitted.', 'your-text-domain' ),
                $k,
                $email,
                $allowed_domain
            );
        }
    }

    // Always return the validation_errors array, even if it's modified.
    return $validation_errors;
}
add_filter( 'ulgm_bulk_add_new_user_validation', 'my_custom_bulk_user_email_domain_validation', 10, 4 );
?>

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/management-group-multi-add-users.php:82
src/classes/group-management/management-group-multi-add-users.php:110

'user_email' => $email,
									'user_id'    => $user_id,
									'first_name' => $first,
									'last_name'  => $last,
									'role'       => $role,
								);

								$validation_errors = apply_filters( 'ulgm_bulk_add_new_user_validation', array(), $user_data, 'bulk_add_users', $k );

								if ( is_array( $validation_errors ) && ! empty( $validation_errors ) ) {
									$error_results = array_merge( $error_results, $validation_errors );
									continue;
								}

								if ( isset( $user_data['first_name'] ) && ! $is_existing ) {


Scroll to Top