Skip to content

Instantly share code, notes, and snippets.

  • Save hazratbilal0079/6ceab6090fe950f9fe563ef1d1c7dc1a to your computer and use it in GitHub Desktop.
Save hazratbilal0079/6ceab6090fe950f9fe563ef1d1c7dc1a to your computer and use it in GitHub Desktop.
Store Last Login Time in user Meta fields and Check User Status from a specific user Meta Field on Login Action through hook or Code Snippet in WordPress
--Hook Name
storelogin
--PhP Hook
add_action( 'jet-form-builder/custom-action/storelogin', function( $request, $action_handler ) {
// Get the user email from the request
$user_email = $request['login_email'];
// Get the user data by email
$user_data = get_user_by('email', $user_email);
// Check if the user exists
if ( $user_data ) {
$user_id = $user_data->ID;
// Get the current time in MySQL format
$current_time = current_time('mysql');
// Update the 'last-login' meta field for the user
update_user_meta($user_id, 'last-login', $current_time);
// Get the 'user-account-status' meta field
$user_status = get_user_meta($user_id, 'user-status', true);
// Check the value of 'user-account-status' and set the response accordingly
if ( $user_status === 'Active' ) {
$action_handler->result = true;
} elseif ( $user_status === 'Inactive' ) {
$action_handler->result = false;
wp_send_json_error([
'hook_result' => false,
'status' => 'failed',
]);
exit;
}
} else {
// Handle the case where the user does not exist
$action_handler->result = false;
wp_send_json_error([
'hook_result' => false,
'status' => 'Contact Administrator',
]);
exit;
}
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment