{"id":15121,"date":"2020-10-31T15:00:11","date_gmt":"2020-10-31T14:00:11","guid":{"rendered":"https:\/\/qodeinteractive.com\/magazine\/?p=15121"},"modified":"2020-11-12T14:35:37","modified_gmt":"2020-11-12T13:35:37","slug":"how-to-manage-cookies-in-wordpress","status":"publish","type":"post","link":"https:\/\/qodeinteractive.com\/magazine\/how-to-manage-cookies-in-wordpress\/","title":{"rendered":"How to Manage Cookies in WordPress"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><p>[vc_row][vc_column][vc_column_text]Love them or hate them, but cookies are an inextricable part of the digital landscape. They are responsible for much of the convenience we experience when browsing or shopping online. They can save us time. Increasingly, cookies can help websites provide a personalized user experience.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]On the other hand, cookies can be intrusive to the point of being as creepy as digital files can get. They can also pose security concerns. For the company that runs the world\u2019s foremost digital advertising giant, cookies are a tool they can leverage to put additional pressure on the competition.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Besides knowing your way around their many (mis)uses, you should also know how to manage WordPress cookies on your website.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]In this article, we\u2019ll show you:[\/vc_column_text][vc_empty_space height=&#8221;22px&#8221;]<ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\"><a href=\"#what-are-cookies\">What Are Cookies<\/a><\/span>        <\/div>\n            <\/li>\n<\/ul>[vc_empty_space height=&#8221;5px&#8221;]<ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\"><a href=\"#does-wordpress-use-them\">Does WordPress Use Them?<\/a><\/span>        <\/div>\n            <\/li>\n<\/ul>[vc_empty_space height=&#8221;5px&#8221;]<ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\"><a href=\"#how-to-manage-cookies\">How to Manage Cookies<\/a><\/span>        <\/div>\n            <\/li>\n<\/ul>[vc_empty_space height=&#8221;68px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\"><a id=\"what-are-cookies\"><\/a>What Are Cookies?<\/h2>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;24px&#8221;]<div class=\"qodef-single-image-holder    \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"400\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/10\/What-Are-Cookies.jpg\" class=\"attachment-full size-full\" alt=\"What Are Cookies?\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/10\/What-Are-Cookies.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/10\/What-Are-Cookies-300x171.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/10\/What-Are-Cookies-620x354.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]When someone visits your website, a small text file containing some information is stored in their web browser. <strong>This file is called a cookie, and it can hold a variety of information<\/strong>. At the very least, a WordPress cookie should have a name and a value. <strong>Some cookies, however, store enough information about the user to enable their creators to follow the users\u2019 online activity even when they browse off to another website.<\/strong>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Cookies aren\u2019t the only type of file that can be used to track people\u2019s online activity. Tracking pixels, small transparent images that can even be embedded in emails, are a standard tracking tool employed by marketers. <strong>Cookies differ from pixels by actually being central to decent user experience, even though they can be used in unsavory ways.<\/strong>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]You can divide cookies into four different groups:<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;22px&#8221;]<ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\"><strong>Session cookies<\/strong> \u2014 cookies that last as long as your browsing session, as closing the browser deletes them.<\/span>        <\/div>\n            <\/li>\n<\/ul><ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\"><strong>Persistent cookies<\/strong> \u2014 these will live through the closing of a browser, with their lifespan limited by the parameters you use with them.<\/span>        <\/div>\n            <\/li>\n<\/ul><ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\"><strong>Third-party cookies <\/strong>\u2014 the cookies served by third-party services through your website.\n<\/span>        <\/div>\n            <\/li>\n<\/ul><ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\"><strong>First-party cookies<\/strong> \u2014 the cookies served by the website you\u2019re visiting.<\/span>        <\/div>\n            <\/li>\n<\/ul>[vc_empty_space height=&#8221;28px&#8221;][vc_column_text]You can also divide cookies by their purpose into the strictly necessary cookies, preference cookies, statistics cookies, and marketing cookies.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Because they can store quite a lot of information about users, <strong>cookies are a subject to local, national, and supranational regulations<\/strong>. GDPR, the <a href=\"https:\/\/qodeinteractive.com\/magazine\/wordpres-gdpr-compliance\/\">EU\u2019s General Data Protection Regulation<\/a>, is an example of a law that touches on the use of cookies and the data they can gather.[\/vc_column_text][vc_empty_space height=&#8221;80px&#8221;][vc_widget_sidebar sidebar_id=&#8221;new-top-picks-banner&#8221;][vc_empty_space height=&#8221;81px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\"><a id=\"does-wordpress-use-them\"><\/a>Does WordPress Use Cookies?<\/h2>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;24px&#8221;]<div class=\"qodef-single-image-holder    \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"451\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/10\/Does-WordPress-Use-Cookies.jpg\" class=\"attachment-full size-full\" alt=\"Does WordPress Use Cookies?\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/10\/Does-WordPress-Use-Cookies.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/10\/Does-WordPress-Use-Cookies-300x193.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/10\/Does-WordPress-Use-Cookies-620x399.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]As a core feature, WordPress relies on two types of cookies. <strong>The users\u2019 cookies are the type of cookies that store the information that identifies you and your status as a logged-in user<\/strong>. This prevents you from having to log in multiple times during the same session, and it can also help with personalization.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]If your website visitors don\u2019t have to log in, your admin team will be the only ones that will receive this cookie. <strong>Because the cookie uses hashed data, you don\u2019t have to worry about someone seeing your username and password if they got their hands on the cookie containing them.<\/strong>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]The other type of cookie that comes with WordPress natively is the commenters\u2019 cookie. <strong>This cookie is used to store the information the visitors need to enter before commenting<\/strong>. You should <a href=\"https:\/\/qodeinteractive.com\/magazine\/gdpr-checkbox-in-wordpress\/\">add an opt-in checkbox<\/a> for GDPR compliance if you plan to use these cookies with EU audiences.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<strong>Your website will not be limited to using only these two types of cookies<\/strong>. The themes you install, the plugins you use, and the advertising and marketing services you subscribe to can also add cookies to your visitors\u2019 browsers. <strong>You can quickly jump from using two types of cookies to using twenty just by installing a couple of plugins.<\/strong>[\/vc_column_text][vc_empty_space height=&#8221;68px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\"><a id=\"how-to-manage-cookies\"><\/a>How to Manage Cookies<\/h2>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;24px&#8221;]<div class=\"qodef-single-image-holder    \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"451\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/10\/How-to-Manage-Cookies.jpg\" class=\"attachment-full size-full\" alt=\"How to Manage Cookies\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/10\/How-to-Manage-Cookies.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/10\/How-to-Manage-Cookies-300x193.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/10\/How-to-Manage-Cookies-620x399.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]For the most part, managing cookies on your website will entail learning about the cookies your plugins and themes use and letting the visitors know that you are using plugins. <strong>Again, you should do this so that you\u2019re compliant with the regulatory framework you and your audiences belong to.<\/strong>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]For example, if you\u2019re using a <a href=\"https:\/\/qodeinteractive.com\/theme-category\/online-store-wordpress-themes\">store WordPress theme<\/a> and you\u2019re relying on WooCommerce to power it, <strong>you should know that WooCommerce uses eight different cookies, five of which are session plugins<\/strong>.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]As for managing your cookies, you can do three different things with them. <strong>You can set them, get the information from them, and delete them.<\/strong>[\/vc_column_text][vc_empty_space height=&#8221;72px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h3 class=\"qodef-h5\">How to Set WordPress Cookies<\/h3>\n<p>[\/vc_column_text][vc_column_text]<strong>The principal thing you should know about setting WordPress cookies is that you\u2019ll do it by editing the functions.php file<\/strong>. This means that you\u2019ll have to be comfortable with <a href=\"https:\/\/qodeinteractive.com\/magazine\/wordpress-create-child-theme\/\">creating a child theme<\/a> and that you should know a bit about PHP.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<strong>You set a cookie in WordPress by using the setcookie () function<\/strong>. The syntax would look something like this:[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]setcookie (parameters divided by comma)<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]The parameters you can use include:[\/vc_column_text][vc_empty_space height=&#8221;22px&#8221;]<ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\">HTTP\/HTTPS \u2013 whether you send cookies via HTTP or HTTPS<\/span>        <\/div>\n            <\/li>\n<\/ul><ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\">The name of the cookie<\/span>        <\/div>\n            <\/li>\n<\/ul><ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\">The value of the cookie<\/span>        <\/div>\n            <\/li>\n<\/ul><ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\">Expiration date expressed in seconds<\/span>        <\/div>\n            <\/li>\n<\/ul><ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\">Path \u2013 the URL path necessary for sending the cookie<\/span>        <\/div>\n            <\/li>\n<\/ul><ul class=\"qodef-unordered-list-item qodef-toc\">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\">Domain \u2013 defines the domain and subdomains where the cookie is available\n<\/span>        <\/div>\n            <\/li>\n<\/ul>[vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<strong>Only the name and the value are mandatory<\/strong>. The remaining parameters are optional. Here\u2019s an example of a cookie created using the init hook to ensure that the placement occurs before the header is sent:[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function tiny_cookie() {\r\nsetcookie( \u2018cookie-name\u2019, 'test', time() + 5 * 604800 );\r\n}\r\nadd_action( 'init', 'tiny_cookie' );<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]With this, we\u2019ve set a cookie that stores the value \u201ctest\u201d on the visitor\u2019s computer and lasts for five weeks.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;72px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h3 class=\"qodef-h5\">How to Get Cookies<\/h3>\n<p>[\/vc_column_text][vc_column_text]<strong>When you\u2019re \u201cgetting\u201d a WordPress cookie, you are retrieving its information<\/strong>. Setting a cookie and not retrieving its information defeats the purpose of setting the cookie in the first place.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]There are a couple of things we\u2019ll need here. <strong>We\u2019ll first need to make sure the cookie was set, and we do that using the isset () function<\/strong>. We\u2019ll use the $_COOKIE variable to narrow down on our cookie, and the echo () function to display the values.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Here\u2019s what that would look like:[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\u00a0if( ! isset( $_COOKIE['cookie-name'] ) ) {\r\necho \"The cookie: '\" . \"cookie-name\" . \"' is not set.\";\r\n} else {\r\necho \"The cookie '\" . \"cookie-name\" . \"' is set.\";\r\necho \"Value of cookie: \" . $_COOKIE['cookie-name'];\r\n}\r\n}\r\nadd_action( 'init', 'tiny_cookie_check' );<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]In case the cookie was set, you\u2019ll be notified it was set, and you\u2019ll see the visitor username. If the cookie wasn\u2019t set, you\u2019ll be informed about it.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;72px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h3 class=\"qodef-h5\">How to Delete Cookies<\/h3>\n<p>[\/vc_column_text][vc_column_text]<strong>When it\u2019s time to delete the cookie, you can unset it using the unset () function, or make it expire by setting its expiry date in the past.<\/strong> You can use both at the same time:[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function unset_tiny_cookie() {\r\nunset( $_COOKIE['cookie-name'] );\r\nsetcookie( 'cookie-name' , 'test', - 3600 );\r\n}\r\nadd_action( 'init', 'unset_tiny_cookie' );<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]With that, you\u2019ve done all you can with WordPress cookies.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;68px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\">Let\u2019s Wrap It Up!<\/h2>\n<p>[\/vc_column_text][vc_column_text]The use of cookies today, as well as the way they are legally regulated, is a topic we will not see cool down anytime soon. But don\u2019t let their lousy rap dismay you from using them \u2014 cookies are essential for creating a good user experience.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]If you\u2019re careful and you practice due diligence, you can administrate your website for years and always use cookies ethically. Remember that, whether you\u2019re relying on third-party cookies or you\u2019re setting your own, the fact that you\u2019re doing it on your website makes you responsible for the cookies\u2019 usage.<br \/>\n[\/vc_column_text][\/vc_column][\/vc_row]<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Having trouble managing WordPress cookies? Usually, you don&#8217;t have to give them a second thought, but if you, here&#8217;s how you can handle them.<\/p>\n","protected":false},"author":9295,"featured_media":15123,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[21,4,13],"class_list":["post-15121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-php","tag-tips","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts\/15121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/users\/9295"}],"replies":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/comments?post=15121"}],"version-history":[{"count":0,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts\/15121\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/media\/15123"}],"wp:attachment":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/media?parent=15121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/categories?post=15121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/tags?post=15121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}