PHP Zone,Wordpress Zone

Using Cookies in WordPress Site: The Why and How Of It?

28 Oct , 2015  

Introduction to Cookies in WordPress :

No matter, how much time and efforts you’ve invested in creating a good-looking WordPress website, it might still fail to get the desired attention. Bear in mind, it’s crucial to address all your customers’ needs, just as it is important to create an aesthetically pleasing site. Needless to say, understanding your customer needs can help you serve them in a better manner, as you’ll get a fair idea of what they might expect from your website.

Put it simply, it’s imperative to track your viewers’ behavior on your site to deliver an optimal user experience.

You may probably start searching for some paid software or professional services to keep a tab on your visitors’. However, such solutions can prove a costly affair. But, what if you’ve a limited development budget?

As a startup firm, you’ll certainly look out for cost-effective ways to gather information about your customers. One of the easiest ways that can assist you in collecting data about your site visitors for free is to make use of cookies.

In simple terms, cookies are tiny pieces of information placed on the browser of a user. WordPress makes use of cookies to legitimate the identity of users that are logged into your site. However, the problem is that cookies get saved in the user’s browser once they’ve approved to store them in their browser window.

The purpose of this post is to help you understand: how you can set cookies in your WP website? But let us first have a glimpse of a few benefits that you’ll get by using cookies in your site.

A Look at the Benefits of Using Cookies in WP Website :

As discussed previously the major benefit that cookies provide is in collecting your visitors (and customers) data. Using the data, you can analyze your customers behavior and understand their habits. Most importantly, getting access to such information, you might be able to deliver expected solutions and an overall better website experience on your online users.

For example, visitors having an interest in knowing about your products and/or services will most likely choose to fill out forms on your WordPress website with details, including their name, contact info, etc. In that case website using cookies will have greater chances of getting approval from users to store their personal information. Doing so, will save your users from having to fill out the form again on their revisit to your site.

Setting Cookies in WordPress Site :

Irrespective of whether you own a small size WordPress site or are running a complex website, you’ll certainly want to know about all of your new users. One best way to do so is to set a cookie on your site with the help of setcookie() function.

Keep in your mind that your WordPress website cookie should be sent together with all HTTP headers. Or else, you will get a warning message that will be visible on your WordPress admin dashboard screen and looks something like:

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

In order to avoid such a warning to be displayed on your dashboard screen, all you have to do is to run the setcookie() function within any of the two functions, namely: init or add_action.

Here’s a code snippet that will help in detecting all new visitors of your site:

//add this code in your theme functions.php file
function set_newuser_cookie() {

if ( !is_admin() && !isset($_COOKIE['sitename_newvisitor'])) {

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

}

}

add_action( 'init', 'set_newuser_cookie');

In this code, we are setting a cookie with the name “sitename_newvisitor”. Also, we have predefined the time after which the cookie will expire (i.e. two weeks). If you want to avoid your cookie from getting expired, then simply change ‘1’ to ‘0’.

Once you’ve set the cookie on your WordPress site, you can use it to provide your users (both existing and new) a little bit distinct information using the below provided code:

if (isset($_COOKIE['sitename_newvisitor'])) {

echo 'We're glad to have you again!';

}

else {

echo 'Welcome to our site!';

}

Conclusion :

If you run a WordPress site, then setting up cookies can help you in improving your user experience. Reading this post will help you understand the benefits of having cookies enabled in a website. Plus, you’ll get to know how you can set cookies to obtain information of new visitors.

Jack Calder is a consistent & enthusiastic professional in Markupcloud Ltd. He has some responsibilities for converting html to wordpress website. Jack is a highly experienced WordPress professional and generally research on various methods.

, , , ,