Using Cookies in WordPress | @thetorquemag
“All The Word That's Fit To Press.”
A WP Engine
Publication
Feature

How To Use Cookies In WordPress

Cookies have existed ever since the inception of the internet. While the use of cookies has had its share of debates and discussions, these remain the standard method of retaining settings and providing a more versatile browsing experience for users.

Much like any other technology or software that is used to build websites, WordPress too interacts and works with cookies for offering custom settings and features to the users.

How does one use cookies in WordPress?

Cookies In WordPress

What Are Cookies?

If you have had a moderate level of experience with the web in general, you might already be aware of cookies and their purpose. Essentially, a cookie is a piece of information that is sent by a website to the user’s web browser, containing a specific set of details. These details can pertain to a lot many things — login or logout sessions, website appearance or customizations, items added to the shopping cart, etc.

When the given user visits the same website again using the same web browser, the browser responds back with the cookie, telling the website to revert to the specific set of configuration, such as a previous browsing session, etc.

Cookies can be encrypted, and can have their own custom date of expiry — say, “log out after 30 days” cookie will expire after 30 days.

Naturally, cookies can be used to enhance the visitor’s browsing experience and improve the overall performance of your website.

How To Set Cookies In WordPress

Setting cookies in WordPress can be accomplished using the setcookie() function, that accepts a number of parameters and sets the cookie accordingly. Here is how the syntax looks like:

setcookie (name, value, expire, path, domain)

For example:

Here we are creating a sample function, torque_setcookie() that invokes the setcookie() function and passes the values for the setting of the cookie. The COOKIEPATH and COOKIE_DOMAIN are specified by WordPress itself, whereas DAYS_IN_SECONDS is a constant value.

Now, if you ever wish to alter an already set cookie, you can just re-invoke this function to set a new cookie with new values.

How To Get Cookies In WordPress

Getting a cookie in PHP means we check whether it has been set or not and if it is set, we retrieve its value. Note that once a cookie has been set and sent in an HTTP header, its value is automatically URL-encoded, and when you retrieve a cookie, its value is automatically decoded too.

The $_COOKIE array variable is often used to retrieve cookies that have already been set.

To get cookies in WordPress, we will first check whether it has been set, using the isset()  function, and if it has been set, we again pass its value to the $_COOKIE associative array. Consider this sample code:

In the above code, we first pass the cookie to the isset() function, to verify whether it has been set or not. If it is not set, we echo an error. Otherwise, implying that it has been set, we echo the value of the cookie with the help of the $_COOKIE variable.

How To Delete Cookies In WordPress

Deleting cookies in WordPress is a very straightforward process.

If, for example, the $_COOKIE array is still holding a cookie value, you just unset it. Like, for our previous example, we will unset it as:

This will suffice; however, to properly “delete” a cookie, you can just call the setcookie() function again but this time pass some vague values. For example:

What did the above code do? It caused the cookie to unset, theoretically, by setting its value to an empty one and then passing a time from the past.

Note that after deleting cookies, you might wish to redirect your users to a custom page or the homepage, for instance, after “log out of all sessions,” the user is generally sent back to the login page.

Conclusion

These are the basic functions that you can perform with cookies in WordPress.

For additional resources, you can refer to the official WordPress Codex about use of cookies in WordPress.

Sufyan bin Uzayr writes for various magazine and blogs, and has authored several books. He blogs about technology, Linux and open source, mobile, web design and development, typography, and Content Management Systems at Code Carbon. You can learn more about him, follow him on Twitter or friend him on Facebook and Google+.

More WordPress News From Torque:

There are no comments

Your email address will not be published. Required fields are marked *

TOP