Filter uncanny-learndash-groups

ulgm_group_key_pattern

Filters the regular expression pattern used to validate group keys, allowing customization of accepted characters.

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

Description

Filters the regular expression pattern used to validate group keys during registration. Developers can modify this pattern to allow or disallow specific characters in group keys. The default pattern permits alphanumeric characters and hyphens only.


Usage

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

Return Value

The filtered value.


Examples

/**
 * Modify the allowed characters for group keys to include underscores.
 *
 * The default pattern for group keys only allows alphanumeric characters and hyphens.
 * This filter allows us to add underscores to the allowed characters for more flexibility
 * in naming groups.
 *
 * @param string $pattern The current regular expression pattern for group keys.
 * @return string The modified regular expression pattern.
 */
add_filter( 'ulgm_group_key_pattern', function( $pattern ) {
    // The original pattern is '/^[A-Za-z0-9-]+$/D'.
    // We want to allow underscores, so we'll modify the character class.
    // Adding '_' to the character class [A-Za-z0-9-] becomes [A-Za-z0-9-_].
    // The 'D' modifier at the end means that the dollar sign ($) must match at the end of the string,
    // after any newline characters. This is usually not desired for simple patterns,
    // and we'll remove it for broader compatibility.
    $pattern = '/^[A-Za-z0-9_-]+$/'; // Allows alphanumeric, hyphen, and underscore.

    return $pattern;
}, 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/group-management-registration.php:321

if ( empty( $code_redeem ) ) {
			//It's empty!
			$this->ulgm_registration_errors()->add( 'code_invalid', Config::$invalid_code );

			return;
		}
		$pattern = apply_filters( 'ulgm_group_key_pattern', '/^[A-Za-z0-9-]+$/D' );
		if ( ! preg_match( $pattern, $code_redeem ) ) {
			// Only alpha numeric and hypen allowed.
			$this->ulgm_registration_errors()->add( 'code_invalid', Config::$invalid_code );

			return;
		}

Scroll to Top