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.
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.
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.
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:
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.