Using WordPress ‘nonce_user_logged_out’ PHP filter

The nonce_user_logged_out WordPress PHP filter allows you to modify whether the user who generated the nonce is considered logged out.

Usage

add_filter( 'nonce_user_logged_out', 'my_custom_nonce_user_logged_out', 10, 2 );
function my_custom_nonce_user_logged_out( $uid, $action ) {
    // Your custom code here
    return $uid;
}

Parameters

  • $uid: int, ID of the nonce-owning user.
  • $action: string|int, The nonce action, or -1 if none was provided.

More information

See WordPress Developer Resources: nonce_user_logged_out

Examples

Change the logged-out user ID

This example changes the logged-out user ID to a custom value.

add_filter( 'nonce_user_logged_out', 'change_logged_out_user_id', 10, 2 );
function change_logged_out_user_id( $uid, $action ) {
    // Set the custom user ID
    $custom_user_id = 999;

    return $custom_user_id;
}

Always consider a specific user as logged out

This example considers a specific user with ID 5 as always logged out.

add_filter( 'nonce_user_logged_out', 'specific_user_always_logged_out', 10, 2 );
function specific_user_always_logged_out( $uid, $action ) {
    if ( $uid === 5 ) {
        return 0;
    }
    return $uid;
}

Set a custom user ID based on the action

This example sets a custom user ID based on the action.

add_filter( 'nonce_user_logged_out', 'set_custom_user_id_based_on_action', 10, 2 );
function set_custom_user_id_based_on_action( $uid, $action ) {
    if ( $action === 'my_custom_action' ) {
        return 100;
    }
    return $uid;
}

Ignore logged-out status for specific action

This example ignores the logged-out status for a specific action, keeping the user logged in.

add_filter( 'nonce_user_logged_out', 'ignore_logged_out_status_for_action', 10, 2 );
function ignore_logged_out_status_for_action( $uid, $action ) {
    if ( $action === 'my_custom_action' ) {
        return -1;
    }
    return $uid;
}

Use a custom function to determine user ID

This example uses a custom function to determine the user ID, based on the action.

add_filter( 'nonce_user_logged_out', 'use_custom_function_for_user_id', 10, 2 );
function use_custom_function_for_user_id( $uid, $action ) {
    // Custom function to get the user ID
    function get_custom_user_id( $action ) {
        // Your custom code to get the user ID
    }

    $new_uid = get_custom_user_id( $action );
    return $new_uid;
}