• I want to create a custom cookie with custom name only for customers who have once logged in. It should be created after they successfully logged in. It should be deleted after they logged out completely. I am using this code below. For some reason it is not working. Can someone throw some light?

    
    // To Add Cookie
    add_action('wp_login', 'add_custom_cookie');
    function add_custom_cookie() {
      if(is_user_logged_in()) {
        setcookie('cookie_name', 'cookie value');
      }
    }
    
    // To Remove Cookie
    add_action('wp_logout', 'remove_custom_cookie');
    function remove_custom_cookie() {
      setcookie('cookie_name', 'cookie value');
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi, smartdiyer, evidently the wp_login action hook doesn’t have access to the current user after doing some experimentation.

    Side note: is_user_logged_in() relies on wp_get_current_user()

    Since that’s the case, even if the user is logged in, your code that sets the cookie will never be executed.

    Thread Starter smartdiyer

    (@smartdiyer)

    @plantprogrammer
    Thanks for Reply and your experimentation.
    Is there a workaround for this.? Like I already said, I need WP to generate a cookie after the user logs in. It should be present only until the user logs out.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Setting a Custom Cookie in WordPress only for Logged In User (non-admin)’ is closed to new replies.