0

I'm doing a database query in a page, and afterwards I want to store an id - I thought I could do it in $_COOKIE?

But it doesn't seem to work to just create a parameter and set a value to it, like:

$_COOKIE['member_id'] = $wpdb->insert_id;

How can I simply add a key and value to the cookie array?

1 Answer 1

2

Use setcookie() like shown below:

setcookie(
  $name, 
  $value, 
  time() + 3600, 
  COOKIEPATH, 
  COOKIE_DOMAIN
);


Note: $wpdb->insert_id AFAIK will always return false or 1, so I'm not sure if that will be of value for your. I might be wrong about that see @s_ha_dum's comment.

6
  • 1
    The Codex is inconsistent about insert_id, at one point saying what you do and at another saying the autoincrement value of the inserted row is returned, which is what I remember.
    – s_ha_dum
    Commented Nov 2, 2015 at 13:53
  • @s_ha_dum You are right about the codex, the latter, the auto-increment value. would indeed make much more sense. Commented Nov 2, 2015 at 13:57
  • Just the name of the variable makes more sense ;)
    – s_ha_dum
    Commented Nov 2, 2015 at 14:01
  • @s_ha_dum Its not like variable names are a guarantee for anything! ;) Commented Nov 2, 2015 at 14:03
  • @ialocin can I declare this function anywhere (no need for actions or filters)? Can I then access $name with $_COOKIE['name']? Btw I read that setcookie was deprecated, now its "wp_set_auth_cookie".
    – Galivan
    Commented Nov 2, 2015 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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