How WordPress uses Cookies

  • Jack
  • 0
  • Aug 19, 2015
  • Web Development

For achieving success in your online business venture, it is important that all your customers’ needs are met in the most efficient manner possible. Of course, knowing about your target audience can help you address all their requirements (from your website). Perhaps, you may look for some paid software and services to track your customers’ behavior on your website. However, do keep in mind that opting for such solutions can cost you a dime.

WordPress Cookies

Needless to say, as a startup or a small business owner, you might want to use a cost-effective way to obtain information about your customers. One such great way that can help you gather data about your website visitors (And customers) is to use cookies.

WordPress uses cookies (i.e. tiny pieces of information that is stored on a user’s browser), to confirm the identity of logged-in users (and commenting users) in your site. However, cookies get stored on your users’ browser only after their approval. Each cookie helps store data about users for a predetermined time frame.

Understanding the Benefits of Using Cookies in WordPress Site

If you own a WordPress site, then using cookies in your website, you can keep a tab on your online users, collect information about those users and learn about their behavior on your site. Getting access to such sort of information can help you improve your users experience at the same time.

For example, customers interested in purchasing your products or services will likely have to fill out forms with details like their name, contact number and so on. Now, if you’re using cookies on your site, then they’ll store this personal information, thereby saving users time from having to fill their personal details repeatedly every time they make a purchase from your site.

What’s more? Cookies prove an effective tool for a web developer and site owner alike, as they help them map users’ behavior to provide better user experience. Moreover, cookies are pretty easy to setup and does not require too much coding knowledge.

How You Can Set Cookies in Your WordPress Site?

No matter, whether you’re running just a small WordPress site or an overly complex website, you may want to detect all the new users that are visiting your site and may also want to present them with some information. You can do so, by setting a cookie on your WordPress website using the default WordPress function: setcookie().

Remember that your website cookie need to be sent along with all of the HTTP headers. If not, then you’ll receive a warning message on your WordPress install that looks something like:

Warning: “Cannot modify header information – headers already sent by (output started at /home/yourtheme/public_html/header.php:5) in /home/yourtheme/public_html/wp/wp-includes/pluggable.php on line ..”

You can avoid such warning from appearing at the first place, simply by running the setcookie() function inside of “init” or “add_action” functions. For example, the below code when added in functions.php file and executed will help you detect new visitors (users) of your website.

function set_newuser_cookie() { if ( !is_admin() && !isset($_COOKIE['yourwebsitename_newvisitor'])) { setcookie('yourwebsitename_newvisitor', 1, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false); } } add_action( 'init', 'set_newuser_cookie');
function set_newuser_cookie() {
  if ( !is_admin() && !isset($_COOKIE['yourwebsitename_newvisitor'])) {
      setcookie('yourwebsitename_newvisitor', 1, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false);
  }
}

add_action( 'init', 'set_newuser_cookie');

The above code snippet is setting a new cookie named as “yourwebsitename_newvisitor” which will expire within two weeks. But, changing ‘1’ to ‘0’ won’t let your website cookie expire.

Using this cookie, you can present your returning user (or new one) somewhat different information using the following code:

if (isset($_COOKIE['yourwebsitename_newvisitor'])) { echo 'It's good to see you again!'; } else { echo 'Welcome new visitor!'; }
if (isset($_COOKIE['yourwebsitename_newvisitor'])) {
  echo 'It's good to see you again!';
} else {
  echo '
Welcome new visitor!';
}

Conclusion

Reading this post will help gain valuable insight on using cookies in WordPress. We have also presented a simple example that demonstrates how you can setup a cookie in your WordPress site.

Related Posts

Written by Jack

With Effective plans, blueprints and strategies Jack Calder is a good professional associated with Markupcloud Ltd- a nice psd to html service company with top-notch results. Jack also shares the prominent knowledge regarding innovative technologies.

View all posts by: