Table of Contents
What are cookies?
How to use cookies on a typical WordPress website
How to set cookies in WordPress
How to get a cookie and use it in WordPress
Delete Cookies in WordPress
Home CMS Tutorial WordPress How to set, get and delete WordPress cookies (like a professional)

How to set, get and delete WordPress cookies (like a professional)

May 12, 2025 pm 08:57 PM
css wordpress Browser tool ai red

Do you want to know how to use cookies on your WordPress website?

Cookies are useful tools for storing temporary information in users' browsers. You can use this information to enhance the 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 professional.

How to set, get and delete WordPress cookies (like a professional)

Note: This is an advanced tutorial. It requires you to be proficient in HTML, CSS, WordPress websites and PHP.

What are cookies?

Cookies are plain text files created and stored in the user's browser when a user visits a website. You can use cookies to add different features to your WordPress website.

Here are some common use cases for cookies:

  • Store and manage user login information
  • Store temporary session information during user access
  • Remember shopping cart merchandise during user visits e-commerce stores
  • Track user activity on the website to provide a personalized user experience

As you can see, cookies are a very useful tool for website owners, but can also be somewhat intrusive. The latest trends in email marketing, growth hacking and online marketing allow websites to set cookies, act as beacons, and can be used to save and even share user activity across websites.

This is why the EU enacts the EU Cookie Act, which requires website owners to declare that they use cookies to store information.

You can learn how to do this on your own website in our guide on how to add cookies to GDPR/CCPA.

How to use cookies on a typical WordPress website

By default, WordPress uses cookies to manage logged-in user sessions and authentication, and remembers the user's name and email address as the user fills out the comment form.

However, many WordPress plugins on your website may also set their own cookies.

For example, OptinMonster allows you to display different email selection forms to new visitors and return visitors, which is achieved by using cookies.

If you use external web services on your website, such as Google Analytics or Google AdSense, then they may also set third-party cookies on your website.

You can view all website cookies in your browser settings. For example, in Google Chrome, you need to first open the Settings page.

You can do this by clicking the 3 dots icon in the upper right corner and selecting Settings or chrome://settings in the address bar.

How to set, get and delete WordPress cookies (like a professional)

On the Settings page, you need to search for Content Settings.

Under "Content Settings", you need to click "Cookies".

How to set, get and delete WordPress cookies (like a professional)

This will open the cookie settings page.

Next, you need to click on the "View all cookies and site data" option.

How to set, get and delete WordPress cookies (like a professional)

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

You can enter the website address in the search box and you will see the data stored on the website.

How to set, get and delete WordPress cookies (like a professional)

Clicking on a single item will show you more detailed information about individual cookies and their content.

How to set cookies in WordPress

To learn this tutorial, you need to add the code to the functions.php file of the topic or use a code snippet plugin such as WPCode. If you haven't done this before, check out our guide on how to copy and paste code snippets in WordPress.

First, we will use the function in PHPsetcookie(). This function accepts the following parameters:

  • Cookie name
  • Cookie value
  • Expiration – Optionally, set the time period for which the cookie expires
  • Path – Optional, using the root directory of the site by default
  • Domain name – optional, the domain name of your website is used by default
  • Security – Optional, if true, cookie data is transmitted only over HTTPS
  • httponly – Optional, when set to true, cookies can only be accessed via HTTP and cannot be used by scripts

Now, let's add code snippets to your WordPress site. This code stores the exact timestamp of a user's visit to your website in a cookie:

 functionwpb_cookies_tutorial1() { $visit_time= date('F j, Y g:i a');if(!isset($_COOKIE[wpb_visit_time])) {// set a cookie for 1 yearsetcookie('wpb_visit_time', $visit_time, time() 31556926);}}
Copy after login

Depend on

Use it with one click in WordPress

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

Now that we have created this cookie, it will be stored in the user's browser for a year, let's see how this information is used on our website.

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

 functionwpb_cookies_tutorial2() {// Time of user's visit$visit_time= date('F j, Y g:i a');// Check if cookie is already setif(isset($_COOKIE['wpb_visit_time'])) {// Do this if cookie is setfunctionvisitor_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 existfunctionvisitor_greeting() {$string.= 'New here? Check out these resources...';return$string;} // Set the cookiessetcookie('wpb_visit_time', $visit_time, time() 31556926);}// Add a shortcodeadd_shortcode('greet_me', 'visit_greeting');}add_action('init', 'wpb_cookies_tutorial2');
Copy after login

Depend on

Use it with one click in WordPress

We have added comments to the code to show you what each section does. This code uses the information stored in the cookie and outputs it using a short code.

You can now add a shortcode [greet_me] anywhere on the website, which will show the last time the user visited.

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

Delete Cookies in WordPress

So far, we've learned how to set cookies and use it later on your website. Now, let's see how to delete cookies.

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

 functionwpb_cookies_tutorial2() {// Time of user's visit$visit_time= date('F j, Y g:i a');// Check if cookie is already setif(isset($_COOKIE['wpb_visit_time'])) {// Do this if cookie is setfunctionvisitor_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 timeunset($_COOKIE['wpb_visit_time']); return$string;} } else{// Do this if the cookie doesn't existfunctionvisitor_greeting() {$string.= 'New here? Check out these resources...';return$string;}}add_shortcode('greet_me', 'visitor_greeting');// Set or Reset the cookiesetcookie('wpb_visit_time', $visit_time, time() 31556926);}add_action('init', 'wpb_cookies_tutorial2');
Copy after login

Depend on

Use it with one click in WordPress

As you can see, this code deletes the cookie once we use the information stored there. We then set cookies again using the updated time information.

We hope this article helps you understand how to easily set, get, and delete WordPress cookies. You may also want to check out our guide on common WordPress errors and how to fix them, as well as the best analytical solutions that our experts have selected for WordPress users.

The above is the detailed content of How to set, get and delete WordPress cookies (like a professional). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
How to register in the ok exchange in China? ok trading platform registration and use guide for beginners in mainland China How to register in the ok exchange in China? ok trading platform registration and use guide for beginners in mainland China May 08, 2025 pm 10:51 PM

In the cryptocurrency market, choosing a reliable trading platform is crucial. As a world-renowned digital asset exchange, the OK trading platform has attracted a large number of novice users in mainland China. This guide will introduce in detail how to register and use it on the OK trading platform to help novice users get started quickly.

AI and Composer: Enhancing Code Quality and Development AI and Composer: Enhancing Code Quality and Development May 09, 2025 am 12:20 AM

In Composer, AI mainly improves development efficiency and code quality through dependency recommendation, dependency conflict resolution and code quality improvement. 1. AI can recommend appropriate dependency packages according to project needs. 2. AI provides intelligent solutions to deal with dependency conflicts. 3. AI reviews code and provides optimization suggestions to improve code quality. Through these functions, developers can focus more on the implementation of business logic.

Top 10 cryptocurrency exchanges in the currency circle, the latest ranking of the top 10 digital currency trading platforms in 2025 Top 10 cryptocurrency exchanges in the currency circle, the latest ranking of the top 10 digital currency trading platforms in 2025 May 08, 2025 pm 10:45 PM

Ranking of the top ten cryptocurrency exchanges in the currency circle: 1. Binance: Leading the world, providing efficient trading and a variety of financial products. 2. OKX: It is innovative and diverse, supporting a variety of transaction types. 3. Huobi: Stable and reliable, with high-quality service. 4. Coinbase: Be friendly for beginners and simple interface. 5. Kraken: The first choice for professional traders, with powerful tools. 6. Bitfinex: efficient trading, rich trading pairs. 7. Bittrex: Safety compliance, regulatory cooperation. 8. Poloniex and so on.

Detailed Guide to Installation and Registration of Binance Binance Exchange (2025 Latest Steps) Detailed Guide to Installation and Registration of Binance Binance Exchange (2025 Latest Steps) May 08, 2025 pm 11:06 PM

Binance is one of the world's leading cryptocurrency trading platforms, providing trading services for a variety of digital assets. If you are considering using Binance for cryptocurrency trading, this article will provide you with a detailed installation and registration guide.

Ouyi ios official website entrance okx Ouyi official website Apple mobile phone registration entrance Ouyi ios official website entrance okx Ouyi official website Apple mobile phone registration entrance May 08, 2025 pm 11:09 PM

If you are an Apple mobile phone user and are interested in cryptocurrency trading, then you must not miss the OKX Ouyi platform. As one of the world's leading cryptocurrency exchanges, OKX Ouyi provides trading services for a variety of digital assets, covering mainstream currencies such as Bitcoin, Ethereum, Litecoin, etc., and also supports the transaction of a variety of altcoins and emerging tokens. Whether you are a freshly-made investor or an experienced trader, OKX Ouyi can meet your needs. Below we will introduce in detail how to note on the official website of OKX Ouyi through Apple mobile phones

Does Binance have a mobile app? Binance binance official website mobile version Android APP recommendation Does Binance have a mobile app? Binance binance official website mobile version Android APP recommendation May 08, 2025 pm 10:12 PM

Binance, as the world's leading cryptocurrency trading platform, provides a variety of ways for users to trade and manage assets easily. Among them, Binance Mobile APP is one of the tools chosen by many users. The following details the download and use of Binance's official Android APP.

Yiou official website entrance Yiou exchange Apple entrance official entry Yiou official website entrance Yiou exchange Apple entrance official entry May 08, 2025 pm 11:12 PM

A world-renowned digital currency trading platform, established in 2014, is committed to providing users with safe and convenient digital asset trading services. As an international exchange, Yiou supports the transaction of a variety of mainstream and niche digital currencies, attracting users from all over the world. Whether newbies or experienced traders, you can find trading tools and services that suit you on the Yiou platform.

Binance binance web version entrance Binance binance exchange web version directly enter Binance binance web version entrance Binance binance exchange web version directly enter May 08, 2025 pm 11:03 PM

The world's leading cryptocurrency trading platform is famous for its efficient, secure and diverse trading services. Whether you are an experienced trader or a newbie into the cryptocurrency market, Binance has the tools and resources you need. Through Binance web version, users can easily access the trading platform without downloading any applications and conduct trading operations directly through the browser. This article will introduce in detail how to enter the Binance Binance Exchange web version and provide some practical trading tips and precautions.

See all articles