Filter uncanny-automator-pro

uap_maybe_generate_anonymous_user_password

Filters the password generated for anonymous users before it is assigned.

add_filter( 'uap_maybe_generate_anonymous_user_password', $callback, 10, 1 );

Description

Allows modification of anonymous user data before password generation. Developers can alter user login, first name, last name, or email. This hook fires when an anonymous user's data is prepared for password creation.


Usage

add_filter( 'uap_maybe_generate_anonymous_user_password', 'your_function_name', 10, 1 );

Parameters

$user_data (mixed)
This parameter holds the generated password for the anonymous user, which can be modified by the filter.

Return Value

The filtered value.


Examples

<?php
/**
 * Filter to modify the password generation for anonymous users.
 *
 * This example demonstrates how to intercept the automatically generated password
 * and potentially modify it, for instance, by adding a prefix or suffix based on
 * user data.
 *
 * @param string  $password    The generated password by wp_generate_password().
 * @param array   $user_data   An array containing user data, including email, first name, last name, etc.
 * @return string The modified or original password.
 */
add_filter( 'uap_maybe_generate_anonymous_user_password', 'my_custom_anonymous_user_password', 10, 2 );

function my_custom_anonymous_user_password( $password, $user_data ) {

	// Example: If the user has a specific role, add a prefix to the password.
	if ( ! empty( $user_data['role'] ) && 'contributor' === $user_data['role'] ) {
		// Prepend 'contrib_' to the generated password.
		$password = 'contrib_' . $password;
	}

	// Example: If the user's email contains 'test.com', append a suffix.
	if ( ! empty( $user_data['user_email'] ) && str_contains( $user_data['user_email'], 'test.com' ) ) {
		// Append '_test' to the generated password.
		$password .= '_test';
	}

	// You could also implement more complex logic, like checking for existing users
	// or integrating with an external password management system.

	// Always return the password, whether modified or not.
	return $password;
}
?>

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

uncanny-automator-pro/src/core/includes/automator-pro-recipe-process-complete.php:513

// @TODO: Move this to separate method.
		$user_data                       = array();
		$user_data['first_name']         = key_exists( 'firstName', $data ) ? $data['firstName'] : '';
		$user_data['last_name']          = key_exists( 'lastName', $data ) ? $data['lastName'] : '';
		$user_data['user_email']         = key_exists( 'email', $data ) ? $data['email'] : '';
		$user_data['user_login']         = key_exists( 'username', $data ) ? $data['username'] : $user_data['user_email'];
		$user_data['role']               = key_exists( 'role', $data ) ? $data['role'] : apply_filters( 'uap_default_user_role', get_option( 'default_role', 'subscriber' ) );
		$user_data['user_pass']          = key_exists( 'password', $data ) ? $data['password'] : apply_filters( 'uap_maybe_generate_anonymous_user_password', wp_generate_password(), $user_data );
		$user_data['prioritized_field']  = key_exists( 'prioritizedField', $fields ) ? $fields['prioritizedField'] : '';
		$user_data['unique_field_value'] = key_exists( 'uniqueFieldValue', $data ) ? $data['uniqueFieldValue'] : '';
		$user_data['unique_field']       = key_exists( 'uniqueField', $fields ) ? $fields['uniqueField'] : '';

		// Validate the required fields are not empty
		$user_data  = $this->maybe_sanitize_user_data( $user_data );
		$validation = $this->validate_user_data_before_new_user_creation( $user_data, $fallback, $user_action );


Scroll to Top