How to Set, Get, and Delete WordPress Cookies (like a Pro)

Do you want to learn how to use cookies on your WordPress site? Cookies are a useful tool to store temporary information in user’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.

How to set, get, and delete cookies in WordPress

Note: This is an advanced tutorial. It requires you to have proficient understanding of HTML, CSS, WordPress site, and PHP.

What Are Cookies?

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.

Following are some of the common usage of cookies on various websites.

  • Store and manage user’s login information
  • Store temporary session information during a users visit
  • Ecommerce stores use cookies to remember cart items during a user’s visit
  • Track user activity on a site to offer personalized user experience
  • and more

As you can see, cookies are highly useful tool for website owners, but they can also be a bit invasive. Recent trends in email marketing, growth hacking, 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.

This is why European Union enacted the EU Cookie Law, which requires website owners to declare that they use cookies to store information.

How Cookies are Used in a Typical WordPress Website

By default, WordPress uses cookies to manage logged-in user sessions and authentication. It also uses cookies to remember a user’s name and email address if they fill out a comment form.

However, many WordPress plugins on your website may also set their own cookies. For example, OptinMonster allows you to show different email optin forms to new vs returning visitors, and it does that by using cookies.

If you are using third party services on your website like Google Analytics or Google AdSense, then they may also set cookies on your website.

You can view all website cookies in your browser’s settings. For example, in Google Chrome you need to go to settings and search for ‘content settings’.

Content settings in Google Chrome

Under content settings, you will need to click on ‘Cookies’ to open the cookies settings page.

Cookies section in Chrome settings

Next, you need to click on the ‘All cookies and site data’ option.

View all cookies and site data

On the next page, you will see a list of all cookies and site data stored on your browser by all websites you visited.

You can type a website address in the search box, and it will show you the data stored by that website.

View site cookies

Clicking on a single item will show you more details about individual cookies and their contents.

How to Set a Cookie in WordPress

To follow this tutorial, you will need to add code to your theme’s functions.php file or a site-specific plugin. If you haven’t done this before, then please take a look at our guide on how to copy and paste code snippets in WordPress.

First we will use the setcookie() function in PHP. This function accepts the following parameters.

  • Cookie name
  • Cookie value
  • Expire (Optional: sets a time period after which cookie expires)
  • Path (Optional, by default it will use the site’s root)
  • Domain (Optional, by default uses your website’s domain)
  • Secure (Optional, If true then only transfers cookie data via HTTPS)
  • httponly (Optional, when set true the cookie is only accessible via HTTP and cannot be used by scripts)

Now let’s add a code snippet to your WordPress site. This code stores the exact timestamp when a user visited your website in a cookie.

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);

}

} 

You can now visit your website and then check your browser cookies. You will find a cookie with the name wpb_visit_time.

How to Get a Cookie and Use it in WordPress

Now that we have created this cookie that’s stored in user’s browser for 1 year, let’s take a look at how can we use this information on our website.

If you know the name of a cookie, then you can easily call it anywhere in PHP using the $_COOKIE[] variable. Let’s add some code that not only sets the cookie but also uses it to do something on your website.

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');

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.

Feel free to modify the code to make it more useful for your website. For example, you can show recent posts to returning users and popular posts to new users.

Deleting a Cookie in WordPress

So far we have learned how to set a cookie and use it later in your website. Now let’s take a look at how to delete a cookie.

To delete a cookie, you need to add the following line to your code.

unset($_COOKIE['wpb_visit_time']);

Don’t forget to replace wpb_visit_time with the name of the cookie you are trying to delete.

Let’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.

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');

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.

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 extremely useful tricks for the WordPress functions file.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

The post How to Set, Get, and Delete WordPress Cookies (like a Pro) appeared first on WPBeginner.

Weekly WordPress News: TechCrunch Headless Redesign With React + WordPress

This week, TechCrunch launched a redesign in the “form of a single page style React app, decoupled from its WordPress backend.” A pretty cool look at what you can do with WordPress and the REST API. Their post announcing the redesign is definitely an interesting read if you’re into WordPress. We also collected a bunch ... Read moreWeekly WordPress News: TechCrunch Headless Redesign With React + WordPress

The post Weekly WordPress News: TechCrunch Headless Redesign With React + WordPress appeared first on Learn WordPress with WPLift.

10 WordPress Plugins Using Artificial Intelligence and Machine Learning

Do you want to use artificial intelligence and machine learning technologies on your WordPress site?

Many WordPress plugins are using AI and machine learning to offer smarter features. There is a chance that you might already be using one of these plugins on your site.

In this article, we will share some of the best WordPress plugins that use artificial intelligence and machine learning.

WordPress Plugins Using Artificial Intelligence and Machine Learning

What are Artificial Intelligence and Machine Learning?

If you follow the news, then you must have heard the words artificial intelligence or machine learning being mentioned.

Artificial intelligence and machine learning are computer technologies that offer smarter solutions in computer devices, apps, GPS devices, smart TVs, home assistance devices, and more.

Artificial intelligence gives computer programs able to make choices without human intelligence.

Machine learning helps the software learn from experiences, data, and user activity to improve artificial intelligence and make better choices.

These technologies allow developers to automate tasks that would normally need human intelligence.

For example, voice recognition, search patterns, translations, image recognition, and more are currently being used by most modern mobile phones and smart devices.

What’s the difference between artificial intelligence and machine learning?

Artificial intelligence can make decisions based on programmed intelligence.

Machine learning further enhances artificial intelligence by self-learning through user activity and collecting information. This makes artificial intelligence smarter and allows it to make more accurate decisions.

What about machines taking over the world?

Many prominent scientists, engineers, and entrepreneurs have expressed concern over the possible threat to human society from artificial intelligence and self-learning machines.

However, it also has great potential to improve the lives of billions of people. That’s why even the most prominent AI skeptics encourage its careful usage for the greater good.

What Can Artificial Intelligence Do for WordPress Websites?

It can provide a smarter and much better user experience for your users.

For example, it can improve WordPress search, grammar and style checking, show related content, improve conversions, boost eCommerce sales, and more.

Your website becomes smarter, faster, and more personalized for your users. This saves you time which allows you to do other things to grow your business.

That being said, let’s take a look at the WordPress plugins that use AI and machine learning.

1. Akismet

Akismet

Akismet is one of the only two plugins that come pre-installed on every WordPress website. It uses artificial intelligence and machine learning to detect spam comments.

Used by millions of websites, Akismet has improved over the years to detect almost all spam submitted by bots. It can also detect spam comments manually submitted by actual human users by learning from their mistakes and recognizing patterns across millions of websites.

For more details, see our Akismet guide for beginners with instructions on how to set it up on your WordPress website.

2. WordLift

WordLift

The WordLift plugin integrates its web-based service to your WordPress website. It offers SEO tips to improve your content with the help of its knowledge graph.

It analyzes your content to offer facts, figures, and media to match and build contextual relationships among your articles which results in better content recommendations. It builds a knowledge graph for your own website, which becomes smarter as you add more content.

3. Grammarly

Grammarly

Grammarly is one of the most popular WordPress plugins. It is an artificial intelligence-powered grammar-checking tool that analyzes the readability of your content to help you write better.

It supports American, British, Canadian, and Australian English languages. It is also available as a Google Chrome addon as an alternative to the JetPack module.

4. Google Language Translator

Google Language Translator

Google Language Translator lets your users easily translate your website content without creating a multilingual WordPress site. It brings Google’s AI and machine learning translation engine to your WordPress website.

You can simply select languages you want to display on your website. After that, you can add the translator to your website using the sidebar widget or shortcode.

For detailed instructions, see our guide on how to add Google Translate to WordPress.

5. MyCurator Content Curation

MyCurator

MyCurator is a cloud-based content curation service. It allows you to add an artificial intelligence-powered content aggregator, which shows content relevant to your selected topics and improves results by learning from what you add or vote on.

You can create a curated content section on your website by fetching the best bits of content relevant to your audience. You can add any article you like to your website with an excerpt and a link back to the source.

6. WebsiteVoice

WebsiteVoice

WebsiteVoice is a free WordPress plugin that converts your text content into high-quality audio. This helps improve user engagement and save time for users to listen to your articles while they’re busy with other routine work.

You’ll need their website token to activate the text-to-speech option on your site. These tokens are free initially, but you have to upgrade to a premium plan to continue using their service.

7. Quttera Web Malware Scanner

Quttera

Quttera Web Malware Scanner allows you to scan your WordPress site for malware and malicious code. This is particularly useful if you suspect that your WordPress site is hacked or blacklisted by search engines.

The plugin uses artificial intelligence to learn from false positives and improves scan results based on usage. While it helps scan an infected website, it doesn’t protect your website from getting affected.

For better security, see our ultimate WordPress security guide to protect your website against hacks, DDOS attacks, and malicious code.

8. ChatBot

ChatBot

ChatBot provides AI-powered virtual chat agent for your website. It uses machine learning to improve the live chat experience and send accurate responses.

You need to sign up for a free account on the plugin’s website. After that, it will start learning as you use it. You can also train it on what to say and how to respond to user queries. This can be handy in keeping users engaged on your website and generating more leads.

9. Link Whisper

LinkWhisper

LinkWhisper is a free WordPress internal linking plugin powered by Artificial Intelligence. It learns from the content on your site and suggests powerful internal linking opportunities to improve your search rankings and grow your traffic.

The plugin looks into your website’s content, including pages and posts. Then it offers suggestions in the WordPress block editor to add internal links while you’re writing and editing an article.

10. ImageSEO

ImageSEO

ImageSEO is a free WordPress image optimization plugin. It uses artificial intelligence to optimize the alt text and title of your images to grow your organic traffic.

The plugin also generates social cards for Twitter, Facebook, and channels. After activating the plugin, you can upload images on the WordPress website, and ImageSEO will write the name, title, and tag for the images automatically. It seamlessly works on single and bulk image uploads.

We hope this article helped you learn about WordPress plugins using artificial intelligence and machine learning. You may also want to see our guide on how to improve your WordPress website speed and performance.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

The post 10 WordPress Plugins Using Artificial Intelligence and Machine Learning first appeared on WPBeginner.

How To Add Facebook Comments To WordPress: 2 Easy Methods

Want to add Facebook comments to your WordPress site instead of the native WordPress comments? While there are plenty of plugins to help you enhance the native WordPress comments system, you might want to completely switch things up and move to Facebook instead. In this post, I’ll show you how to add Facebook comments to ... Read moreHow To Add Facebook Comments To WordPress: 2 Easy Methods

The post How To Add Facebook Comments To WordPress: 2 Easy Methods appeared first on Learn WordPress with WPLift.

How to Restore the Get Shortlink Button in WordPress

Do you want to create a shortlink of your WordPress blog posts or pages? WordPress shortlinks are useful for sharing in your emails, facebook messages, or even text messages. It was a default feature until WordPress version 4.4. Recently, one of our readers asked how to bring back the get shortlink feature in WordPress. In this tutorial, we will share how to restore the get shortlink button in WordPress.

How to Get the Get Shortlink Button in WordPress

What is the Get Shortlink Button in WordPress?

Get Shortlink button is a WordPress feature that allows you to easily get a short link of your WordPress blog post or page. It used to be located right next to the permalink area in the post edit screen.

Get Shortlink Button

However if you recently started your blog, then you will not see this feature on your WordPress site because it was removed after WordPress version 4.4.

We find this feature to be quite useful for easily getting shortlinks for our blog posts that look like this:

http://wpbeg.in/2csN1pj

If you want to bring back the get shortlink feature on your WordPress site, then you’re in the right place.

Let’s take a look at how to bring back the get shortlink button back in WordPress.

Method 1: Using WP Bitly plugin to Add Get Shortlink Button

This method is easier and recommended for most users. This method also allows you to create custom branded short URLs for your website.

First thing you need to do is install and activate the WP Bitly plugin. For more details, see our step by step guide on how to install a plugin in WordPress.

Upon activation, this plugin will automatically add the Get Shortlink button on your post edit screen.

Get Shortlink Button

Once you click on this button, it will show you the shortlink for your WordPress posts and pages. This shortlink can be shared in your emails and messages, so your audience can reach directly to the page.

Page Shortlink

You can also use the WP Bitly plugin to create custom branded shortlinks for your blog posts like we do on WPBeginner. See our step by step guide on how to create your own customized short url in WordPress.

Method 2: Manually Adding Get Shortlink Button

This method requires you to add code to your WordPress files. If you haven’t done this before, then please take a look at our guide on how to copy and paste code in WordPress.

You will need to add the following code to your WordPress theme’s functions.php file or a site-specific plugin:


add_filter( 'get_shortlink', function( $shortlink ) {return $shortlink;} );

This code will add the shortlink filter to display the Get Shortlink button in your WordPress pages and posts.

Get Shortlink Button

You can click on this button to get the shortlink for your post or page.

We hope this article helped you bring back the Get Shortlink button in WordPress. You may also want to check out our list of the must have WordPress plugins for business websites.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

The post How to Restore the Get Shortlink Button in WordPress appeared first on WPBeginner.

Weglot Review: Fully Translate Your WordPress Site In Just A Couple Minutes

If you need to translate your WordPress site, there’s a good chance you’ve come across a plugin called Weglot. Having recently passed 20,000 users and €44,000 in monthly revenue, Weglot isn’t really a newcomer to the translation scene anymore. After using Weglot several times, I can see why they’ve been successful – Weglot is definitely ... Read moreWeglot Review: Fully Translate Your WordPress Site In Just A Couple Minutes

The post Weglot Review: Fully Translate Your WordPress Site In Just A Couple Minutes appeared first on Learn WordPress with WPLift.

MyCryptoCheckout Review: Accept Cryptocurrency on WooCommerce or Easy Digital Downloads

Last time I checked in with you all about cryptocurrency, Bitcoin was soaring high at $18,474.10. Prices have dropped a little since then (a moment of silence for my portfolio), but with a market cap of $372 billion, cryptocurrency is still here to stay. And that fact might have you interested in accepting cryptocurrency payments ... Read moreMyCryptoCheckout Review: Accept Cryptocurrency on WooCommerce or Easy Digital Downloads

The post MyCryptoCheckout Review: Accept Cryptocurrency on WooCommerce or Easy Digital Downloads appeared first on Learn WordPress with WPLift.

10Web Review: WordPress Site Management + 60 Premium Plugins For One Price

10Web is an intriguing new service that aims to make it a heckuva lot easier to manage a fleet of WordPress websites. It’s: Part all-access theme/plugin club Part website management dashboard for multiple WordPress installs So where do those premium themes and plugins come from? Well, I can’t find anything officially linking the two, but ... Read more10Web Review: WordPress Site Management + 60 Premium Plugins For One Price

The post 10Web Review: WordPress Site Management + 60 Premium Plugins For One Price appeared first on Learn WordPress with WPLift.

How to Add YouTube Subscribe Button in WordPress

Do you want to get more subscribers on your YouTube channel? Having a subscribe button on your blog makes it easy for your visitors to find your videos and subscribe to your channel. In this article, we will show you how to add a YouTube subscribe button in WordPress.

How to Add YouTube Subscribe Button in WordPress

Method 1: Manually Adding YouTube Subscribe Button Code

The first thing you need to do is visit the YouTube subscribe button page on the Google Developer’s website.

Configuration Button

On this page, you will need to enter your YouTube channel’s name, select a layout, theme, and subscriber count option. It will display a live preview of how your button will look on the website. Once you are satisfied, simply scroll down to copy the embed code.

Copy Code

Next, you need to login to the WordPress admin area of your site and go to Appearance » Widgets. There you will need to drag and drop a text widget to your widget area, and paste the YouTube subscribe button code inside the widget.

Add YouTube Subscribe Script

Once you are done, don’t forget to save your changes and visit your website to see the YouTube subscribe button in action.

Subscribe to YouTube Channel in Sidebar

Method 2: Using Plugin to Add a YouTube Subscribe Bar

Another way to add a YouTube subscribe button on your website is by adding it below every YouTube video on your site. This offers additional exposure and can help you significantly increase your subscribers.

First you need to install and activate the YouTube Subscribe Bar plugin.

Upon activating the plugin, you need to go to Settings » YouTube Subscribe Bar to add your YouTube channel ID. You will also need to style the button layout, choose a button theme, and show subscribers count.

YouTube Subscribe Bar Settings

The YouTube subscribe bar plugin allows you to add some text before the button and edit its styling. You can use this area to add a call to action like “Subscribe to My Channel”.

Once you are done, make sure to click on the Save Changes button to save your settings.

This plugin will automatically display your YouTube Subscribe bar below every YouTube video on your site. The YouTube Subscribe bar will appear on the videos from your channel and third-party channels, so you can share useful video content on your site and get more subscribers on your YouTube channel.

YouTube Subscribe Bar

Note: The YouTube subscribe bar plugin only works with oEmbed videos. It will not work with videos embedded via iFrame.

We hope this article helped you add the YouTube subscribe button in WordPress. You may also want to check out our guide on how to track user engagement on your WordPress site.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

The post How to Add YouTube Subscribe Button in WordPress appeared first on WPBeginner.