Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use auth_cookie_valid action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
September 18, 2022
5 minutes read

Hey there! So, you’ve landed on the auth_cookie_valid action page. This action fires once an authentication cookie has been validated. To get started with it, you need to register it using add_action. You can throw this code into the functions.php of your activated theme or even better, in a custom WordPress Plugin. We at WePlugins always prefer creating a custom WordPress Plugin for using hooks to ensure nothing breaks when you update your WordPress theme in the future.

Now, let’s dive into some practical examples. Below, we’ve defined a function weplugins_execute_on_auth_cookie_valid_event which takes 2 parameters, and we register it using add_action. The first parameter auth_cookie_valid is the name of the hook, the second parameter weplugins_execute_on_auth_cookie_valid_event is the function that needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you need to remove a registered hook, so you can use remove_action to remove the auth_cookie_valid action.

Parameters

    Below are the 2 parameters required to use this hook:

  • $cookie_elements : (string[]) Authentication cookie components.
    • ‘username’ (string) User’s username.
    • ‘expiration’ (string) The time the cookie expires as a UNIX timestamp.
    • ‘token’ (string) User’s session token used.
    • ‘hmac’ (string) The security hash for the cookie.
    • ‘scheme’ (string) The cookie scheme to use.
  • $user : (WP_User) User object.

Access Premium WordPress Plugins

Live Example

Here’s the hook in action:

do_action( 'auth_cookie_valid', string[] $cookie_elements, WP_User $user )

Example 1: Basic Usage

Below is an example of how you can use this hook:

    function weplugins_execute_on_auth_cookie_valid_event($cookie_elements, $user){
       // You can write code here to be executed when this action occurs in WordPress. Use the parameters received in the function arguments & implement the required additional custom functionality according to your website requirements.
    }
    // add the action
    add_action( "auth_cookie_valid", "weplugins_execute_on_auth_cookie_valid_event" , 10, 2);
    

Example 2: Removing the Hook

To remove a hook callback, use the example below:

remove_action( "auth_cookie_valid", "weplugins_execute_on_auth_cookie_valid_event", 10, 2 );

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Example 3: Custom Functionality

Let’s say you want to log the user’s username when the auth cookie is validated:

    function weplugins_log_username_on_auth($cookie_elements, $user){
        error_log('Username authenticated: ' . $cookie_elements['username']);
    }
    // add the action
    add_action( "auth_cookie_valid", "weplugins_log_username_on_auth" , 10, 2);
    

If you need any customization or run into any issues, feel free to Contact Us. We’re here to help!

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.