0

In August, GDPR requires all websites to have a cookie notification on every page. I am creating a custom post type plugin - but I'm having one problem. The content from the custom post type page isn't being pulled in - it's pulling in the content from the page it is on. If I click on the permalink of the notice page - the content displays exactly as I want it. My guess is, because the plugin is on every page "the_content();" pull in the content from the page they are on. I need to know how to work around this.

Here is the code.

THE PLUGIN CODE:

<?php

/**
* Plugin Name: GDPR Cookie Notice
* Description: Adds a Pop-up notice bar on the bottom of the page for GDPR notifications
**/

defined( 'ABSPATH' ) or die( 'You Shall Not Pass!' );

function gdpr_create_post_type(){
    $labels = array(
        'name'               => 'GDPR Notice', // => seperator for associative array key => value
        'singular_name'      => 'GDPR Notice',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New GDPR Notice',
        'edit_item'          => 'Edit GDPR Notice',
        'new_item'           => 'New GDPR Notice',
        'view_item'          => 'View GDPR Notices',
        'search_items'       => 'Search GDPR Notices',
        'not_found'          => 'No GDPR Notices Found',
        'not_found_in_trash' => 'No GDPR Notices Found in Trash'
    );
    $args = array(
        'labels'             => $labels,
        'has_archive'        => false, // these will be used in pages
        'public'             => true,
        'hierarchical'       => true, // behave like a page
        'rewrite'            => array(
            'with_front'     => false,
            'slug'           => 'gdpr-notice'
        ),
        'menu_icon'          => 'dashicons-id-alt',
        'supports'           => array(
            'title',
            'author',
            'editor',
            'custom-fields',
            'page-attributes'
        )
    );
    register_post_type('gdpr', $args);
}
add_action('init', 'gdpr_create_post_type');

?>

THE HEADER.PHP FILE WHERE THE SINGLE PAGE TEMPLATE IS PULLED IN:

<header>
    ... main navigation code ...
</header>

<?php get_template_part('single', 'gdpr'); ?>

THE SINGLE-GDPR.PHP FILE:

<?php 
/* 
The file for the GDPR Cookie Notice post type, registered via a plugin
*/
?>
<div class="cookie-bar">
    <section class="section float-me me-floated">
        <div class="container">
            <div class="row text-center">

                <!-- I've added both editor and custom field filters to see if I can get even one to work -->

                <?php if ( have_posts()) :
                    while ( have_posts()) : the_post(); 

                        the_content();

                    endwhile; 
                endif; ?>

                <?php if ( have_posts()) :
                    while ( have_posts()) : the_post(); 

                        echo get_post_meta($post->ID, 'notice', true);

                    endwhile; 
                endif; ?>

            </div>
        </div>
    </section>
</div>

WP ADMIN - GDPR Notice custom post type page

I'm trying two options – just to see if I can get one to work

WP Editor: Read our privacy policy to learn more about how we use cookies.

Custom Field: Name: notice Value: Read our privacy policy to learn more about how we use cookies.

Thanks for any help you can give.

0

I used some javascript code to pop up the message, and placed it in my "Simple GDPR' plugin https://wordpress.org/plugins/simple-gdpr/ .

I got the javascript code from https://cookieconsent.insites.com/ , which details how to do it.

I used the 'after_header' action , as in

add_action('after_header', 'my_gdpr_notice');

to display the notice, plus using the 'enqueue_scripts' functions to add the javascript code and the associated CSS for the message.

  • Rick. I decided to replace the SINGLE-GDPR.PHP FILE Wordpress loop by querying the database table in mysql and spitting that out. Do you think that is a good idea or not? – S. Jensen Jul 19 '18 at 15:57
  • Not sure why you have to put your GDPR notice in a post, then query for the post for every page load. My plugin just adds some javascript and stuff to the page; that displays the popup notice. Clicking the notice sets a cookie. if cookie set, no more notice (for a year). And the popup references the Privacy page with a link (the plugin lets you choose a page to be the 'privacy page'). Using a plugin to display the notice saves a query to the database: if the cookie is set, there is no need to display the notice. If no cookie, display the notice. Simple, in my mind. Works easy. Look at my code. – Rick Hellewell Jul 19 '18 at 22:43
  • I wrote a blog entry about how I did it: securitydawg.com/gdpr-and-analytics-and-compliance ; basic code that is added to via the wp-head filter. The plugin does a bunch more, but the code in the blog is a start. – Rick Hellewell Jul 19 '18 at 22:44
  • This is my first plugin so I'm experiencing a bit of a learning curve while developing it. I looked at your plugin and didn't see WPML support - which I need for a multilingual mutlisite. There are plugins that we can purchase licenses for that will cover WPML - but where's the fun in "not" learning how to create a plugin?... Your plugin looks very easy to use, though. – S. Jensen Jul 20 '18 at 17:30
  • Yeah, no multilanguage support in my plugin. I supposed I could add it, but not sure it's worth the effort. I mostly developed it for my own purposes - didn't find a GDPR plugin that I liked; some were too complex, others wanted $. All I needed is a simple popup message, since my sites are US-based. So, built my own...and had 'fun' developing it. – Rick Hellewell Jul 20 '18 at 17:59
0

I decided to make a call to the MySQL database and pull that information in the single-gdpr.php template, so replace the WordPress loop with a database call.

<?php

    $table_prefix  = '###_';
    $startat = 'englishnotice'; // English Custom Field Name
    $sitepostmeta = $table_prefix . 'postmeta';

    $posts = $wpdb->get_results("SELECT meta_value FROM $sitepostmeta WHERE meta_key = '$startat'");

    echo $posts[0]->meta_value;

?>
  • ERP... $posts I think is a reserved variable so use $gdprposts - otherwise your blog page and any other custom post type page won't pull in content. $gdprposts = $wpdb->get_results("SELECT meta_value FROM $sitepostmeta WHERE meta_key = '$startat'"); echo $gdprposts[0]->meta_value; – S. Jensen Jul 25 '18 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.