Add Simple Cookie Consent Notice in WordPress without Plugin

/* Cookie Consent Notice */
/* Download the JS and CSS files and store in root folder */
add_action('wp_footer', 'wpflt_footer_cookie_notice');
function wpflt_footer_cookie_notice(){
?>
<script src="/cookieconsent.min.js" defer></script><script>window.addEventListener("load",function(){window.wpcc.init({"colors":{"popup":{"background":"#000000","text":"#ffffff","border":"#000000"},"button":{"background":"#ffffff","text":"#000000"}},"position":"bottom","content":{"href":"https://example.com/privacy/","message":"We rely on Cookies to Provide Better User Experience","button":"I Understand","link":"Learn More"}})});</script>
<?php
};

Add the above code to your theme functions to display cookie consent notice in WordPress without any plugins. This is a simple cookie consent notice. The only option a user can do is, to accept the consent.

Users cannot select custom cookies and accept the consent. Since we are not marketing any product, this type of notice is sufficient for our site just to notify users that we use third party analytics code to measure site engagement.

Now download the following files, JavaScript and CSS

Open the JS file, search for ‘wpfilters’ URL. Replace with your website’s URL and save it. Once saved, upload both files to root directory. Test it by opening your website in a new window. It works!.

Customize the Background, Text and Border colors in the above PHP snippet. Change the Privacy Page link. Update the message and button text.

Done!. There you have it, a simple light weight cookie consent notice without any heavy plugins. In our case, the total size is just 15KB. If you need any other functionality, better use Plugins.