Set Cookies in wordpress

Using this blow code you can set cookies using wordpress

1.Paste this code where you want to set cookies
Like header.php footer.php or particular page you can set cookies.
 <?php 
if(isset($_COOKIE['AboutVisit'])) 
{ $last = $_COOKIE['AboutVisit']; echo "Welcome back! <br> You last visited on ". $last;
 } else 
{ echo "Welcome to our site!"; 
} 

2.Add below code into Function.php

<?php

  function my_setcookie() {
 $Month = 1000 + time();
 //this adds 30 days to the current time
 setcookie(AboutVisit, date("F jS - g:i a"), $Month);
 }

 add_action('init', 'my_setcookie');?>
Advertisements

One thought on “Set Cookies in wordpress

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s