Filter Dynamic uncanny-learndash-groups

ulgm_filter_var_{dynamic}

> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Filters a variable dynamically based on its type, allowing customization before it's used.

add_filter( 'ulgm_filter_var_{dynamic}', $callback, 10, 1 );

Description

Fires after a variable is checked using `filter_has_var()`. Developers can use this dynamic filter to modify the boolean result of the check, allowing for custom validation logic or to intercept and alter the perceived existence of a variable before it's used. The `$variable` part of the hook name determines its specific context.


Usage

add_filter( 'ulgm_filter_var_{dynamic}', 'your_function_name', 10, 1 );

Parameters

$type (mixed)
This parameter contains the name of the variable being checked.

Return Value

The filtered value.


Examples

/**
 * Example: Sanitize a specific GET parameter to ensure it's a valid email address.
 *
 * This function hooks into 'ulgm_filter_var_user_email' to validate and sanitize
 * the 'user_email' GET parameter. If the parameter is present and can be filtered
 * as an email, it will be returned. Otherwise, it will be null.
 */
add_filter( 'ulgm_filter_var_user_email', 'my_plugin_sanitize_user_email_param', 10, 1 );

function my_plugin_sanitize_user_email_param( $filtered_value ) {
	// The $filtered_value here is the result of the original filter_has_var() call.
	// We want to further process it if it's a valid email.

	// Check if the original filter_has_var returned true for an email type.
	// Note: The original function 'ulgm_filter_has_var' applies the filter
	// *after* filter_has_var. So $filtered_value is actually the result of filter_has_var.
	// To perform actual sanitization, we need to check the original type used in the hook.
	// In a real-world scenario, the hook name itself dictates what's being filtered.

	// For the purpose of this example, let's assume we want to ensure the 'user_email'
	// is not only present but also looks like a valid email format.
	// We'll re-evaluate the original GET variable here for a more robust example.

	$user_email = isset( $_GET['user_email'] ) ? sanitize_email( $_GET['user_email'] ) : null;

	// Now, we can use filter_var to check if it's a valid email format.
	// If the original hook was designed to only check for existence, this adds an extra layer.
	if ( $user_email && filter_var( $user_email, FILTER_VALIDATE_EMAIL ) ) {
		// The original filter_has_var returned true, and we've confirmed it's a valid email format.
		// Return the sanitized and validated email.
		return $user_email;
	}

	// If it wasn't a valid email or if the original filter_has_var returned false.
	return null;
}

/**
 * Example: Ensure a specific POST parameter is a positive integer.
 *
 * This function hooks into 'ulgm_filter_var_product_quantity' to validate and sanitize
 * the 'product_quantity' POST parameter. If the parameter is present and can be filtered
 * as a positive integer, it will be returned. Otherwise, it will be null.
 */
add_filter( 'ulgm_filter_var_product_quantity', 'my_plugin_sanitize_product_quantity_param', 10, 1 );

function my_plugin_sanitize_product_quantity_param( $filtered_value ) {
	// Similar to the email example, the $filtered_value is the result of filter_has_var.
	// We'll re-evaluate the original POST variable for a more practical sanitization.

	$product_quantity = isset( $_POST['product_quantity'] ) ? intval( $_POST['product_quantity'] ) : null;

	// Check if it's a positive integer.
	if ( $product_quantity !== null && filter_var( $product_quantity, FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1 ) ) ) ) {
		// The original filter_has_var likely returned true for the POST variable,
		// and we've confirmed it's a positive integer.
		return $product_quantity;
	}

	// If it wasn't a positive integer or if the original filter_has_var returned false.
	return null;
}

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/global-functions.php:35

function ulgm_filter_has_var( $variable = null, $type = INPUT_GET ) {
	return apply_filters( 'ulgm_filter_var_' . $variable, filter_has_var( $type, $variable ) );
}

Scroll to Top