How to Add a Cookie Consent Popup in WordPress
In this article
Adding a cookie consent popup to your WordPress site helps you comply with privacy laws like GDPR and CCPA by letting visitors choose whether non-essential cookies (e.g., analytics, ads) can be stored. This guide shows you two reliable methods: using a plugin (easiest) or adding custom code (more control). You will need admin access to your WordPress dashboard, and for the custom method, a child theme or a code snippet plugin.
What You'll Need
- WordPress admin access (to install plugins or edit theme files).
- For the plugin method: a free or premium cookie consent plugin (e.g., Cookie Notice, Complianz, or CookieYes).
- For the custom code method: a child theme (so updates don't overwrite your changes) or a code snippets plugin (like WPCode).
- Basic understanding of where to paste code (functions.php or a snippet plugin).
Step-by-Step
- Choose your method. Decide between a plugin (quick, no coding) or custom code (lighter, more privacy-friendly). For most users, a plugin is recommended.
- Install a cookie consent plugin. In your WordPress admin, go to Plugins → Add New. Search for "cookie consent" or "GDPR cookie". Choose a well-rated plugin like Cookie Notice & Compliance for GDPR/CCPA or Complianz. Click Install Now, then Activate.
- Configure the plugin settings. After activation, find the plugin's settings page (usually under Settings or a new menu item). Set the following:
- Message text: Write a clear, concise notice like "We use cookies to improve your experience. By clicking 'Accept', you agree to our use of cookies."
- Accept button text: e.g., "Accept" or "I Agree".
- Decline button text: e.g., "Decline" or "Reject".
- Link to your privacy policy: Enter the URL of your privacy policy page. If you don't have one, create it first.
- Cookie expiry: Set how long the consent lasts (e.g., 30 days or 1 year).
- Position: Choose bottom, top, or center overlay.
- Enable or disable specific cookies (optional). Some plugins let you categorize cookies (necessary, analytics, marketing). If you use Google Analytics, you can set the plugin to block analytics until consent is given. Look for a "Cookie Manager" or "Consent for Services" section.
- Save and test. Click Save Changes. Open your site in a new incognito/private window. You should see the popup. Click both Accept and Decline to ensure they work.
- Custom code method (if not using a plugin). If you prefer code, add the following to your child theme's functions.php or via a code snippet plugin:
Note: This basic code only hides the popup; it does not block cookies. For full compliance, you need to integrate with your analytics script (e.g., only load Google Analytics if consent is 'accepted').// Add cookie consent popup function add_cookie_consent_popup() { ?> <div id="cookie-popup" style="position:fixed;bottom:0;left:0;right:0;background:#333;color:#fff;padding:15px;text-align:center;z-index:9999;"> <p>We use cookies to improve your experience. <a href="/privacy-policy" style="color:#fff;">Learn more</a>.</p> <button style="background:#4CAF50;color:white;border:none;padding:10px 20px;margin:5px;">Accept</button> <button style="background:#ccc;color:black;border:none;padding:10px 20px;margin:5px;">Decline</button> </div> <script> function acceptCookies() { document.getElementById('cookie-popup').style.display = 'none'; localStorage.setItem('cookieConsent', 'accepted'); } function declineCookies() { document.getElementById('cookie-popup').style.display = 'none'; localStorage.setItem('cookieConsent', 'declined'); } window.onload = function() { if (localStorage.getItem('cookieConsent') === null) { document.getElementById('cookie-popup').style.display = 'block'; } }; </script> <?php } add_action('wp_footer', 'add_cookie_consent_popup'); - Integrate with analytics (important). If you use Google Analytics, you must delay loading it until the user accepts. With a plugin like Complianz, this is built-in. With custom code, you would wrap your analytics script in a check for
localStorage.getItem('cookieConsent') === 'accepted'. - Test on mobile and desktop. Ensure the popup is responsive and doesn't block navigation. Check that it appears on all pages, not just the homepage.
Common Mistakes
- Not blocking cookies before consent. Merely showing a popup without actually preventing cookies from being set is non-compliant. Use a plugin that integrates with analytics or manually defer scripts.
- No decline option. Many laws require users to be able to reject non-essential cookies. Always include a clear decline/reject button.
- Forgetting to link your privacy policy. The popup must link to your privacy policy where users can learn more.
- Setting cookie expiry too long. A year is common, but longer may be seen as less compliant. Check your jurisdiction's guidance.
- Placing the popup where it covers important content. Use a bottom bar or a small modal, not a full-screen overlay that annoys users.
Tips
- Use a plugin that supports geo-targeting if you only need consent for EU visitors (e.g., Complianz).
- Keep the message short and friendly. Avoid legal jargon.
- Test your popup in incognito mode after clearing cookies to ensure it reappears.
- If you use caching plugins, exclude the popup from caching so it appears for new visitors.
- Consider adding a "Cookie Settings" link in your footer so users can change their choice later.
Troubleshooting
- Popup doesn't appear: Clear your browser cache and cookies. Check that the plugin is active and the settings are saved. If using custom code, verify the code is correctly pasted in the right file (functions.php or snippet plugin).
- Popup appears but doesn't hide after clicking: Check for JavaScript errors in your browser's console (F12). Ensure the button IDs match the script. If using a plugin, try deactivating other scripts that might conflict.
- Popup appears on every page even after accepting: This usually means the localStorage is not being set. In custom code, ensure the script runs after the DOM is loaded. With a plugin, check if a caching plugin is serving a static version of the page.
- Google Analytics still loads before consent: If using a plugin, enable the "Block scripts" or "Cookie blocking" feature. If using custom code, you must conditionally load the analytics script based on the consent value.
- Popup is not styled correctly: If using a plugin, check the styling options. For custom code, add CSS to match your theme.
Frequently Asked Questions
Do I really need a cookie consent popup?
If your site uses cookies for analytics, advertising, or any non-essential purpose and you have visitors from the EU (GDPR) or California (CCPA), yes. Even if you're not sure, it's safer to have one.
Which is better: plugin or custom code?
Plugins are easier and more reliable, especially for compliance features like blocking scripts. Custom code is lighter but requires more technical knowledge and maintenance.
Can I use a free plugin?
Yes, many free plugins like Cookie Notice or GDPR Cookie Consent provide basic functionality. For advanced features like geo-targeting or script blocking, you may need a premium version.
What should the popup say?
It should state that the site uses cookies, explain why (e.g., "to improve your experience"), and link to your privacy policy. It must include an accept and a decline button.
How long should the cookie consent last?
Common practice is 30 days to 1 year. After expiry, the popup should reappear. Some laws suggest re-consent every 6-12 months.
Will this affect my SEO?
No, a properly implemented cookie popup does not hurt SEO. However, a popup that blocks the entire page might be penalized for poor user experience. Use a non-intrusive design.