GDPR: Cookie-based content blocking


In this blog post, I will show you a simple way to hide content until the user agrees to receive cookies. You can use it to stop loading adverts or other scripts that are collecting user data, to achieve a better GDPR compliance, or you can simply hide the content of your choice, and force the users to accept cookies to view that content. And best of all, the method I propose is free.

The method I propose relies on two plugins from the WordPress repository:

The easiest way to hide content is to use the [cookies_accepted] and the [/cookies_accepted] shortcodes. Just put the content you want to hide between the two shortcodes and the content will be shown only if cookies are accepted.

However, this method does not allow you to hide other shortcodes. Such as the case of shortcodes generated by advertising plugins.

The cookie notice plugin provides a way to code a functionality depending on the cookie notice value using the following  function:

We can easily use this function to trigger a shortcode, using the do_shortcode() WordPress function:

One can change the above code can show an image if cookies are not accepted, replacing the highlighted line with:

Where path_to_your_image is the full path toward an image that will be shown when cookies are not accepted. This is useful to show a placemark holder to prevent your pagination form going wrong when cookies are not accepted.

Using PHPCode snippets to create new shortcodes

The above method is quite simple, but there’s a problem: one cannot run PHP code in widget areas. And here enters the scene the second plugin I’ve mentioned, the Insert PHP Code Snippet. One can use this plugin to transform the above code into a simple shortcode that can be inserted in widget areas. No advanced PHP knowledge necessary, no need to change theme files. Simple and effective.


Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.