<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WPWife &#187; how to set cookies in wordpress</title>
	<atom:link href="http://www.wpwife.com/category/how-to-set-cookies-in-wordpress/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wpwife.com</link>
	<description>WPHelp: fix and optimise WP themes&#38;plugins</description>
	<lastBuildDate>Fri, 21 Nov 2025 12:00:00 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.38</generator>
	<item>
		<title>How to Set, Get, and Delete WordPress Cookies (like a Pro)</title>
		<link>http://www.wpwife.com/tutorials/how-to-set-get-and-delete-wordpress-cookies-like-a-pro</link>
		<comments>http://www.wpwife.com/tutorials/how-to-set-get-and-delete-wordpress-cookies-like-a-pro#comments</comments>
		<pubDate>Mon, 19 Mar 2018 11:55:28 +0000</pubDate>
		<dc:creator><![CDATA[Editorial Staff]]></dc:creator>
				<category><![CDATA[how to set cookies in wordpress]]></category>
		<category><![CDATA[how to use cookies in wordpress]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[wordpress cookies]]></category>

		<guid isPermaLink="false">http://www.wpbeginner.com/?p=50789</guid>
		<description><![CDATA[
<p>Do you want to learn how to use cookies on your WordPress site? Cookies are a useful tool to store temporary information in user&#8217;s browser and then use this information to enhance user experience through personalization and behavioral targeting. In this ultimate guide, we will&#8230;&#160;<strong><a href="http://www.wpbeginner.com/wp-tutorials/how-to-set-get-and-delete-wordpress-cookies-like-a-pro/">Read More &#187;</a></strong></p>
<p>The post <a rel="nofollow" href="http://www.wpbeginner.com/wp-tutorials/how-to-set-get-and-delete-wordpress-cookies-like-a-pro/">How to Set, Get, and Delete WordPress Cookies (like a Pro)</a> appeared first on <a rel="nofollow" href="http://www.wpbeginner.com/">WPBeginner</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Do you want to learn how to use cookies on your WordPress site? Cookies are a useful tool to store temporary information in user&#8217;s browser and then use this information to enhance user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPress cookies like a pro. </p>
<p><img title="How to set, get, and delete cookies in WordPress" src="http://cdn3.wpbeginner.com/wp-content/uploads/2018/02/wpcookiesguide.png" alt="How to set, get, and delete cookies in WordPress" width="550" height="340" class="alignnone size-full wp-image-50802" /></p>
<p><strong>Note:</strong> This is an advanced tutorial. It requires you to have proficient understanding of HTML, CSS, WordPress site, and PHP.</p>
<h4>What Are Cookies?</h4>
<p>Cookies are plain text files that are created and stored in users browser when they visit a website. Cookies are used to add different features on a website. </p>
<p>Following are some of the common usage of cookies on various websites. </p>
<ul>
<li>Store and manage user&#8217;s login information</li>
<li>Store temporary session information during a users visit</li>
<li><a href="http://www.wpbeginner.com/wp-tutorials/how-to-start-an-online-store/" title="How to Start an Online Store in 2018 (Step by Step)">Ecommerce stores</a> use cookies to remember cart items during a user&#8217;s visit</li>
<li>Track user activity on a site to offer personalized user experience</li>
<li>and more</li>
</ul>
<p>As you can see, cookies are highly useful tool for website owners, but they can also be a bit invasive. Recent trends in <a href="http://optinmonster.com/beginners-guide-to-email-marketing/" title="Email Marketing Guide" >email marketing</a>, <a href="https://optinmonster.com/growth-hacking/"  title="Growth Hacking Guide">growth hacking</a>, and online marketing as a whole allow websites to set cookies that act as a beacon and can be used to store and even share user activity across websites. </p>
<p>This is why European Union enacted the <a href="http://www.wpbeginner.com/plugins/add-cookies-popup-wordpress/" title="How to Add a Cookies Popup in WordPress">EU Cookie Law</a>, which requires website owners to declare that they use cookies to store information. </p>
<h4>How Cookies are Used in a Typical WordPress Website</h4>
<p>By default, WordPress uses cookies to manage logged-in user sessions and authentication. It also uses cookies to remember a user&#8217;s name and email address if they fill out a comment form. </p>
<p>However, many <a href="http://www.wpbeginner.com/beginners-guide/what-are-wordpress-plugins-how-do-they-work/" title="What Are WordPress Plugins? And How Do They Work?">WordPress plugins</a> on your website may also set their own cookies. For example, <a href="https://optinmonster.com/"  title="OptinMonster">OptinMonster</a> allows you to show different email optin forms to new vs returning visitors, and it does that by using cookies.</p>
<p>If you are using third party services on your website like <a href="http://www.wpbeginner.com/beginners-guide/how-to-install-google-analytics-in-wordpress/" title="How to Install Google Analytics in WordPress for Beginners">Google Analytics</a> or <a href="http://www.wpbeginner.com/refer/google-adsense/"  title="Google Adsense" rel="nofollow">Google AdSense</a>, then they may also set cookies on your website. </p>
<p>You can view all website cookies in your browser&#8217;s settings. For example, in Google Chrome you need to go to settings and search for &#8216;content settings&#8217;. </p>
<p><img title="Content settings in Google Chrome" src="http://cdn3.wpbeginner.com/wp-content/uploads/2018/02/chrome-settings.png" alt="Content settings in Google Chrome" width="550" height="301" class="alignnone size-full wp-image-50793" /></p>
<p>Under content settings, you will need to click on &#8216;Cookies&#8217; to open the cookies settings page. </p>
<p><img title="Cookies section in Chrome settings" src="http://cdn4.wpbeginner.com/wp-content/uploads/2018/02/cookies.png" alt="Cookies section in Chrome settings" width="550" height="226" class="alignnone size-full wp-image-50794" /></p>
<p>Next, you need to click on the &#8216;All cookies and site data&#8217; option. </p>
<p><img title="View all cookies and site data" src="http://cdn3.wpbeginner.com/wp-content/uploads/2018/02/seeallcookies.png" alt="View all cookies and site data" width="550" height="259" class="alignnone size-full wp-image-50795" /></p>
<p>On the next page, you will see a list of all cookies and site data stored on your browser by all websites you visited. </p>
<p>You can type a website address in the search box, and it will show you the data stored by that website. </p>
<p><img title="View site cookies" src="http://cdn3.wpbeginner.com/wp-content/uploads/2018/02/viewsitecookies.png" alt="View site cookies" width="550" height="241" class="alignnone size-full wp-image-50796" /></p>
<p>Clicking on a single item will show you more details about individual cookies and their contents. </p>
<h4>How to Set a Cookie in WordPress</h4>
<p>To follow this tutorial, you will need to add code to your theme&#8217;s <a href="http://www.wpbeginner.com/glossary/functions-php/" title="What is functions.php File in WordPress?">functions.php</a> file or a <a href="http://www.wpbeginner.com/beginners-guide/what-why-and-how-tos-of-creating-a-site-specific-wordpress-plugin/" title="What, Why, and How-To’s of Creating a Site-Specific WordPress Plugin">site-specific plugin</a>. If you haven&#8217;t done this before, then please take a look at our guide on <a href="http://www.wpbeginner.com/beginners-guide/beginners-guide-to-pasting-snippets-from-the-web-into-wordpress/" title="Beginner’s Guide to Pasting Snippets from the Web into WordPress">how to copy and paste code snippets in WordPress</a>. </p>
<p>First we will use the <code>setcookie()</code> function in <a href="http://www.wpbeginner.com/glossary/php/" title="What is PHP? How WordPress Uses PHP?">PHP</a>. This function accepts the following parameters. </p>
<ul>
<li>Cookie name</li>
<li>Cookie value</li>
<li>Expire (Optional: sets a time period after which cookie expires)</li>
<li>Path (Optional, by default it will use the site&#8217;s root)</li>
<li>Domain (Optional, by default uses your website&#8217;s domain)</li>
<li>Secure (Optional, If true then only transfers cookie data via HTTPS)</li>
<li>httponly (Optional, when set true the cookie is only accessible via HTTP and cannot be used by scripts)</li>
</ul>
<p>Now let&#8217;s add a code snippet to your WordPress site. This code stores the exact timestamp when a user visited your website in a cookie. </p>
<pre class="brush: php; title: ; notranslate">
function wpb_cookies_tutorial1() { 

$visit_time = date('F j, Y  g:i a');

if(!isset($_COOKIE[$wpb_visit_time])) {

// set a cookie for 1 year
setcookie('wpb_visit_time', $current_time, time()+31556926);

}

} 

</pre>
<p>You can now visit your website and then check your browser cookies. You will find a cookie with the name <code>wpb_visit_time</code>. </p>
<h4>How to Get a Cookie and Use it in WordPress</h4>
<p>Now that we have created this cookie that&#8217;s stored in user&#8217;s browser for 1 year, let&#8217;s take a look at how can we use this information on our website. </p>
<p>If you know the name of a cookie, then you can easily call it anywhere in PHP using the $_COOKIE[] variable. Let&#8217;s add some code that not only sets the cookie but also uses it to do something on your website. </p>
<pre class="brush: php; title: ; notranslate">
function wpb_cookies_tutorial2() { 
// Time of user's visit
$visit_time = date('F j, Y g:i a');

// Check if cookie is already set
if(isset($_COOKIE['wpb_visit_time'])) {

// Do this if cookie is set 
function visitor_greeting() {

// Use information stored in the cookie 
$lastvisit = $_COOKIE['wpb_visit_time'];

$string .= 'You last visited our website '. $lastvisit .'. Check out whats new'; 

return $string;
}	

} else { 

// Do this if the cookie doesn't exist
function visitor_greeting() { 
$string .= 'New here? Check out these resources...' ;
return $string;
}	

// Set the cookie
setcookie('wpb_visit_time',  $visit_time, time()+31556926);
}

// Add a shortcode 
add_shortcode('greet_me', 'visitor_greeting');

} 
add_action('init', 'wpb_cookies_tutorial2');
</pre>
<p>We have commented the code to show you what each part does. This code uses the information stored in the cookie and outputs it using the shortcode. You can now add shortcode [greet_me] anywhere on your website, and it will show when a user last visited. </p>
<p>Feel free to modify the code to make it more useful for your website. For example, you can <a href="http://www.wpbeginner.com/wp-tutorials/how-to-display-recent-posts-in-wordpress/" title="How to Display Recent Posts in WordPress">show recent posts</a> to returning users and <a href="http://www.wpbeginner.com/wp-tutorials/display-popular-posts-by-day-week-month-and-all-time-in-wordpress/" title="How to Display Popular Posts by Day, Week, and Month in WordPress">popular posts</a> to new users. </p>
<h4>Deleting a Cookie in WordPress</h4>
<p>So far we have learned how to set a cookie and use it later in your website. Now let&#8217;s take a look at how to delete a cookie. </p>
<p>To delete a cookie, you need to add the following line to your code. </p>
<pre class="brush: php; title: ; notranslate">
unset($_COOKIE['wpb_visit_time']);
</pre>
<p>Don&#8217;t forget to replace wpb_visit_time with the name of the cookie you are trying to delete. </p>
<p>Let&#8217;s put this code in some context using the same sample code we used above. This time we will delete a cookie and set it again with new information. </p>
<pre class="brush: php; title: ; notranslate">
function wpb_cookies_tutorial2() { 
// Time of user's visit
$visit_time = date('F j, Y g:i a');

// Check if cookie is already set
if(isset($_COOKIE['wpb_visit_time'])) {

// Do this if cookie is set 
function visitor_greeting() {

// Use information stored in the cookie 
$lastvisit = $_COOKIE['wpb_visit_time'];

$string .= 'You last visited our website '. $lastvisit .'. Check out whats new'; 

// Delete the old cookie so that we can set it again with updated time
unset($_COOKIE['wpb_visit_time']); 

return $string;
}	

} else { 
// Do this if the cookie doesn't exist
function visitor_greeting() { 
$string .= 'New here? Check out these resources...' ;
return $string;
}	
}
add_shortcode('greet_me', 'visitor_greeting');

// Set or Reset the cookie
setcookie('wpb_visit_time',  $visit_time, time()+31556926);
} 
add_action('init', 'wpb_cookies_tutorial2');

</pre>
<p>As you can see, this code deletes the cookie once we have used the information stored inside. Later we set the cookie again with the updated time information. </p>
<p>We hope this article helped you learn how to easily set, get, and delete WordPress cookies. You may also want to see our list of other <a href="http://www.wpbeginner.com/wp-tutorials/25-extremely-useful-tricks-for-the-wordpress-functions-file/" title="32 Extremely Useful Tricks for the WordPress Functions File">extremely useful tricks for the WordPress functions file</a>.</p>
<p>If you liked this article, then please subscribe to our <a href="http://youtube.com/wpbeginner?sub_confirmation=1" title="WPBeginner on YouTube"  rel="nofollow">YouTube Channel</a> for WordPress video tutorials. You can also find us on <a href="http://twitter.com/wpbeginner" title="WPBeginner on Twitter"  rel="nofollow">Twitter</a> and <a href="https://www.facebook.com/wpbeginner" title="WPBeginner on Facebook"  rel="nofollow">Facebook</a>.</p>
<p>The post <a rel="nofollow" href="http://www.wpbeginner.com/wp-tutorials/how-to-set-get-and-delete-wordpress-cookies-like-a-pro/">How to Set, Get, and Delete WordPress Cookies (like a Pro)</a> appeared first on <a rel="nofollow" href="http://www.wpbeginner.com/">WPBeginner</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wpwife.com/tutorials/how-to-set-get-and-delete-wordpress-cookies-like-a-pro/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="" length="0" type="" />
		</item>
	</channel>
</rss>
