Filter uncanny-automator-pro

automator_pro_generate_random_string_allowed_characters

Filters the characters allowed for random string generation by the Automator Pro generator.

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

Description

Filters the characters used in generating random strings. Developers can modify the available character set to include or exclude specific character types before the string is generated. This allows for customized random string generation based on specific requirements, ensuring control over the output.


Usage

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

Parameters

$allowed_chars (mixed)
This filter allows you to modify the set of characters that can be used when generating a random string.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the automator_pro_generate_random_string_allowed_characters filter hook.
 * This example will remove all special characters from the allowed characters list.
 *
 * @param string $allowed_chars The current string of allowed characters.
 * @return string The modified string of allowed characters.
 */
add_filter( 'automator_pro_generate_random_string_allowed_characters', function( $allowed_chars ) {
	// Remove special characters by replacing them with an empty string.
	// This regex matches common special characters, adjust if needed.
	$special_chars_to_remove = '!#$%&()*+,-./:;<=>?@[]^_`{|}~';
	$allowed_chars = str_replace( str_split( $special_chars_to_remove ), '', $allowed_chars );

	return $allowed_chars;
}, 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

uncanny-automator-pro/src/integrations/generator/actions/uoa-generate-random-string.php:213

public function generate_random_string( $length = 16, $nums = 'false', $lower_abc = 'false', $upper_abc = 'false', $special_chars = 'false' ) {
		if (
			'false' === $nums &&
			'false' === $lower_abc &&
			'false' === $upper_abc &&
			'false' === $special_chars
		) {
			$nums          = 'true';
			$lower_abc     = 'true';
			$upper_abc     = 'true';
			$special_chars = 'true';
		}

		$allowed_chars = '';

		if ( 'false' !== $nums ) {
			$allowed_chars .= apply_filters( 'automator_pro_generate_random_string_numbers', '0123456789' );
		}

		if ( 'false' !== $lower_abc ) {
			$allowed_chars .= apply_filters( 'automator_pro_generate_random_string_lower_alphabets', 'abcdefghijklmnopqrstuvwxyz' );
		}

		if ( 'false' !== $upper_abc ) {
			$allowed_chars .= apply_filters( 'automator_pro_generate_random_string_upper_alphabets', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' );
		}

		if ( 'false' !== $special_chars ) {
			$allowed_chars .= apply_filters( 'automator_pro_generate_random_string_special_chars', '!#$%&()*+,-./:;<=>?@[]^_`{|}~' );
		}

		$allowed_chars = apply_filters( 'automator_pro_generate_random_string_allowed_characters', $allowed_chars );

		return substr( str_shuffle( str_repeat( $allowed_chars, ceil( $length / strlen( $allowed_chars ) ) ) ), 1, $length );
	}


Scroll to Top