Using PHP Cookies in WordPress

Create your php cookies in WordPress

I’ve always been scared of creating PHP cookies. Cookies are an extremely widely used web technology consisting of little pieces of data that live on your device and remember things: for example, the last product you browsed on Amazon, or whether you’re already logged-in to Gmail. As common as they are, though, cookies have always felt like a spooky piece of web architecture that I presumed would be painful and complex to implement.

Then a client project came up that needed cookies, and guess what? They’re easy to write—much easier than, for example, baking cookies, which I still refuse to learn.

In this article, we’ll walk you through why, and how, to use cookies in WordPress.

Why Use PHP Cookies?

There are times when user account creation is too clunky a means to store persistent user data. Cookies can persist user data without requiring a login.

Websites have lots of user-specific data. A site might want to know all kinds of things to improve my user experience: my native language, the last time I visited, form information I’ve previously submitted, and so on. In many cases, this data needs to persist: it needs to last across site visits.

The most common way WordPress uses to persist data about site visitors is through WordPress user accounts. This system is really full-featured, with all kinds of user roles, permissions, data, metadata, and so on.

There are times, though, when it’s a bit too clunky. By default, it requires users to create an account—complete with, at minimum, a username, email, and password—and then log into that account. To just store a bit of very data (like, say, a user language preference or time zone), that’s an awful lot. Cookies can persist user data without requiring a login.

WordPress’s inbuilt cookie system works through its user system, and that’s not what we want. So here we’re kicking it old-school, with pure PHP that we wrap into a plugin.

A Demo PHP Cookie


9 Responses

Comments

  • Hi Fred! Thanks for sharing this nifty technique. This might be a dumb question (it’s probably a dumb question) but if I put in something other than “Cookies” in the demo and submit, shouldn’t the demo field display whatever I submitted the next time I load this page?

    • fredclaymeyer says:

      Hi Sasha, Not a dumb question at all! 🙂 The cookie changes what shows up in the “Your most recent favorite food was ___” line below the form.

      I’d meant to make that line bold to be attention-getting, but missed that when I published—it should be a bit more prominent now.

      Does that make sense?

      • Makes total sense! However, no matter how many times I submit a new answer I’m still being told that I haven’t told you what my favorite food is. Just a glitch on my side (Chrome 45.0.2454.101 m / Windows 10)?

        • fredclaymeyer says:

          Hi Sasha,

          You’re right! The issue is how the cookie interacts with our site’s caching – which turns out to be quite an issue with cookies in general. (I wasn’t noticing it because I was logged-in, and so not seeing the cached version of the site.)

          I’ll write a fix for this up for next week. Thank you!

          Fred

  • Just a quick note: The correct way of setting cookies in WP is this:


    setcookie( 'true_cookie', 1, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN );

    Last two constants ensures that things will work in any possible configuration. Your sample will only work if the WP is installed in the root directly.

Pingbacks