Action uncanny-learndash-toolkit

uo_login_before_validation

Fires before user login credentials are validated, allowing modifications.

add_action( 'uo_login_before_validation', $callback, 10, 1 );

Description

Fires just before login form validation occurs. Developers can use this hook to perform custom checks, manipulate user input, or add pre-validation logic. It's important to note that validation errors should be handled by the `uo_login_before_validation_message` hook.


Usage

add_action( 'uo_login_before_validation', 'your_function_name', 10, 1 );

Examples

<?php
/**
 * Example of using the 'uo_login_before_validation' action hook.
 *
 * This function demonstrates how to hook into the 'uo_login_before_validation'
 * action to perform custom actions just before the login form validation occurs.
 * In this example, we're simulating checking for a specific user role
 * and potentially adding a class to the validation output if the user
 * doesn't meet the criteria.
 */
function my_custom_login_validation_check() {
    // Simulate checking if the user is logged in and has a specific role.
    // In a real-world scenario, you would likely fetch this data from WordPress functions
    // like get_current_user_id(), wp_get_current_user(), etc.
    $current_user = wp_get_current_user();
    $required_role = 'administrator'; // Example required role

    if ( ! in_array( $required_role, (array) $current_user->roles ) ) {
        // If the user doesn't have the required role, you might want to
        // add a message or modify the validation output.
        // For demonstration, we'll just add a class to the body.
        // In a real plugin, you might add data to a global variable
        // that the template can then access.
        //
        // Note: Directly adding CSS classes to the body here via 'body_class'
        // hook is more appropriate, but this example shows how you *could*
        // influence the output within this hook's context by, for example,
        // storing a flag that the template can check.
        //
        // For this specific hook's context (before validation), you'd more likely
        // be preparing data or performing backend checks that affect the validation logic itself.
        //
        // Let's simulate adding a flag that the template might use.
        // In a real scenario, you'd probably pass this data back to the template.
        // This is a simplification for the example.
        add_action( 'uo_login_before_validation_message', function() {
            echo '<p>Warning: You do not have administrative privileges. Some features may be limited.</p>';
        });

        // Alternatively, if you wanted to pass data to the template directly,
        // you might use a global variable or an object that the template can access.
        // Example (not recommended for production without careful consideration):
        // global $my_custom_login_data;
        // $my_custom_login_data['has_required_role'] = false;
    } else {
        // User has the required role.
        // global $my_custom_login_data;
        // $my_custom_login_data['has_required_role'] = true;
    }
}

// Add the custom function to the 'uo_login_before_validation' action hook.
// The '10' is the default priority. '1' indicates that this function accepts 1 argument
// (though in this specific example, we're not directly using arguments passed by the hook).
// If the hook passed arguments, you would define them in the function signature and
// increase the 'accepted_args' value.
add_action( 'uo_login_before_validation', 'my_custom_login_validation_check', 10, 0 );
?>

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/templates/frontend-login/layout_1-login.php:118
src/templates/frontend-login/modern_ui-login.php:118

$css_classes[] = 'ult-form__validation--has-error';
	}

	// Start output
	ob_start();

	// Do uo_login_before_validation
	do_action( 'uo_login_before_validation' );

	?>

	<div class="ult-form__validation <?php echo implode( ' ', $css_classes ); ?>">
		<div class="ult-notice ult-notice--error">
			<?php do_action( 'uo_login_before_validation_message' ); ?>


Scroll to Top