Filter uncanny-learndash-groups

ulgm_key_prefix

Filters the key prefix used for storing plugin options and settings, allowing customization of option naming conventions.

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

Description

Fires when generating random group keys to allow modification of the prefix. Developers can use this filter to prepend custom strings to all generated group keys, ensuring unique or branded key structures. The default value is an empty string.


Usage

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

Return Value

The filtered value.


Examples

/**
 * Add a custom prefix to generated random codes.
 *
 * This function filters the 'ulgm_key_prefix' hook to prepend a specific string
 * to the random codes generated by the Group Management plugin.
 *
 * @param string $prefix The current prefix (initially empty).
 * @return string The modified prefix with our custom string.
 */
add_filter( 'ulgm_key_prefix', function( $prefix ) {
    // Let's add a unique prefix based on a WordPress option or a constant.
    // For demonstration, we'll use a fixed string.
    $custom_prefix = 'UGM_'; // Example: Unique Group Management Prefix

    // You could also dynamically get a prefix, for example:
    // $custom_prefix = get_option('my_ulgm_prefix', 'DEFAULT_');

    return $custom_prefix . $prefix;
}, 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/handlers/class-group-management-db-handler.php:94

public function generate_random_codes( $qty ) {
		$codes  = array();
		$i      = 0;
		$prefix = apply_filters( 'ulgm_key_prefix', '' );
		$suffix = apply_filters( 'ulgm_key_suffix', '' );
		while ( $i < $qty ) {
			$random_code = $this->random_string();
			if ( ! isset( $codes[ $random_code ] ) ) {
				$codes[] = sprintf( '%s%s%s', $prefix, $random_code, $suffix );
			}
			++$i;
		}

		return $codes;
	}


Scroll to Top