0

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');
}

Edit: Using the above code, Cookie is not created at all. BTW, I can manually create cookies just using setcookie(), they work. But, in the code it won't.

2
  • Please edit your question to clarify what you mean when you say "it is not working". Is the cookie not being set on login? Is it not being unset on logout?
    – Pat J
    Jun 9, 2021 at 13:52
  • [After your edit] did you try the code I supplied below? There's a reason your cookies aren't getting set... I outline that
    – Paul G.
    Jun 10, 2021 at 11:28

1 Answer 1

0

There are a few things going on there that need to be tweaked. The code below should do what you need.

  • The setting of the cookie shouldn't query for is the user logged-in. This can't be relied upon to return true on a login request. Just rely on the wp_login hook to assure you that the user is logging in.
  • I've set this with a long cookie lifetime - i.e 1 week. using the WordPress constant WEEK_IN_SECONDS. You can change this by replacing WEEK_IN_SECONDS with a number (of seconds) you want it to last.
  • The deletion of the cookie is done by setting a low/negative value so that it's essentially "expired"
  • Important: Make sure this is added to your functions.php or anywhere you can be sure it will be executed in-time before a login request is processed.
add_action( 'wp_login', 'add_custom_cookie' );
function add_custom_cookie() {
    setcookie( 'cookie_name', 'cookie value', time()+WEEK_IN_SECONDS );
}

// To Remove Cookie
add_action( 'wp_logout', 'remove_custom_cookie' );
function remove_custom_cookie() {
    setcookie( 'cookie_name', '', -1000000 );
}
6
  • Thanks Paul for your efforts and Inputs. What you mentioned in the first point was very helpful. Have a great day
    – baazigar
    Jun 10, 2021 at 11:36
  • Unfortunately, The code is not creating cookie for Admin or Customer. I tried with both types of accounts. The purpose of the code is to create a temporary cookie for the Customer since I have blocked wp-login.php using Cloudflare Rule. If there is a cookie, the rule will be bypassed and customer can Logout.
    – baazigar
    Jun 10, 2021 at 11:42
  • Where are you putting this code on your site?
    – Paul G.
    Jun 10, 2021 at 11:51
  • functions.php of child theme. I currently have disabled Cloudflare firewall too.
    – baazigar
    Jun 10, 2021 at 11:55
  • I've tweaked the cookie delete code slightly. Also, I've tested this in a simple WordPress site by putting the code in the functions.php file and it works exactly as required. Cookie created, cookie deleted. So there must be something else different on your environment that you haven't explained. Also, how are you actually testing the cookie is or isn't there? Have you tried using another tool to check?
    – Paul G.
    Jun 10, 2021 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.