Support » Developing with WordPress » Can we customize “the_content()”?

  • Resolved StephenEquus

    (@stephenequus)


    I am developing a plugin that will add a GDPR notice bar on every page. The problem is pulling in the content from the custom post type (plugin) into the template that is shown on every page. The problem is this line of code WordPress uses to pull in the content from pages/posts:

    <?php if ( have_posts()) :
    while ( have_posts()) : the_post();
    the_content();
    endwhile;
    endif; ?>

    “the_content()” is already being used in every page/post template unless “the_exerpt()” is being used (and I can’t use that – we have excerpts). I need to know what code I can add to the GDPR template that I am including on every page so it pulls in the GDPR custom post content and not the content form the page/post.

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • I’m looking into this page:

    https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

    and taking the top code snippet to see if it will target the GDPR custom post I created to add the content:

    <?php 
    // returns the content of $GLOBALS['post']
    // if the page is called 'debug'
    function my_the_content_filter($content) {
    // assuming you have created a page/post entitled 'debug'
    if ($GLOBALS['post']->post_name == 'GDPR Cookie Notice') {
    return var_export($GLOBALS['post'], TRUE );
    }
    // otherwise returns the database content
    return $content;
    }
    
    add_filter( 'the_content', 'my_the_content_filter' );
    ?>
    • This reply was modified 1 year, 2 months ago by  Steve Stern.

    The above snippet didn’t work.

    Trying to add content using WordPress custom field feature and this code in the GDPR template:

    <?php if ( have_posts()) :
    while ( have_posts()) : the_post(); 
    
    echo get_post_meta($post->ID, 'notice', true);
    
    endwhile; 
    endif; ?>

    Not working. Nothing is coming out on the webpage though.

    • This reply was modified 1 year, 2 months ago by  StephenEquus.

    Perhaps the plugin isn’t recognizing the theme template. Here is what I have so far:

    PLUGIN:

    plugins (folder) -> gdpr-cookie-notice (folder) -> gdpr-cookie-notice.php (file)

    PLUGIN PHP (just one file):

    <?php
    
    /**
    * Plugin Name: GDPR Cookie Notice
    * Description: Adds a Pop-up notice bar on the bottom of the page for GDPR notifications
    * Author: Stephen Jensen
    **/
    
    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, // false removes permalink /blog/ structure
    				'slug'             => 'gdpr-notice'
    			),
    			'menu_icon'          => 'dashicons-id-alt',
    			'supports'           => array( // features
    				'title',
    				'author',
    				'editor',
    				'custom-fields',
    				'page-attributes'
    			)
    		);
    		register_post_type('gdpr', $args);
    	}
    	add_action('init', 'gdpr_create_post_type');
    
    ?>

    THEME:

    theme (folder) -> theme_name (folder) -> single-gdpr.php

    THEME PHP (just one file – trying two different ways to get the content):

    <?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">
    
    				<?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; ?>
    
    				<p style="text-align: center;">
    					/*************************<br />
    					SINGLE-GDPR.PHP<br />
    					*************************/
    				</p>
    
    			</div>
    		</div>
    	</section>
    </div>

    Header.php (link to gdpr so it is on every page):

    <header>... navigation code ...</header>
    <?php get_template_part('single-gdpr'); ?>

    WP ADMIN:

    GDPR Notice (admin menu) -> GDPR Cookie Notice (page name)

    PAGE OPTIONS (I filled two options – just trying to 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.

    Hopefully this shows the method to my madness. Perhaps I missed a step. Thanks for any help anyone can give.

    • This reply was modified 1 year, 2 months ago by  StephenEquus.
    • This reply was modified 1 year, 2 months ago by  StephenEquus.
    • This reply was modified 1 year, 2 months ago by  StephenEquus.
    • This reply was modified 1 year, 2 months ago by  StephenEquus.

    I click on the permalink for the page – GDPR Cookie Notice – and I can tell it is using the single-gdpr.php template in the themes directory and it is pulling in both the_content() and the echo get_post_meta($post->ID, 'notice', true); so some where it is failing when it gets pulled into the header.php file. In the source code I can see the html code from single-gdpr.php but no content is being pulled in.

    Moderator bcworkz

    (@bcworkz)

    I’m sorry, I didn’t look through all of your code examples and I only lightly skimmed through your posts. This is because I don’t think “the_content” is the best way to put up GDPR notices. If you added the notice to every post content occurrence, on an index or archive page, you will end up with many multiples of the same notice on one page.

    Most people want the notice to occur as the first thing in the page header. The problem for plugin devs is there is no reliable action or filter we can hook to cause output as first thing in the header. Probably the most reliable hook is “wp_nav_menu”. This will work for the vast majority of themes. You can tack the GDPR notice before or after the menu HTML just before it’s output.

    The other problem with using “wp_nav_menu” is you cannot be sure how extra content will affect header layout. The notice would need to be absolute positioned in order to be least likely to affect layout. It would be more like a modal than page content.

    With absolute positioned content, the actual HTML can occur anywhere on the page, even the footer. You could use the “wp_footer” action to cause the notice to be output. Indeed, even “the_content” could be used provided you take measures to ensure it is only output once on a page and not with every post. This can be done by having your callback remove itself from the filter stack after the initial output.

    I decided to query the database to grab the information form the table created for this post type and spit that out. Do you think this is a good solution or not?

    Moderator bcworkz

    (@bcworkz)

    Get the GDPR notice verbiage from a custom post type post? Yes, that’s good. Then it’s relatively easy for someone to alter the message if need be. That’s assuming the ability to alter the message is important. It would be more efficient to hardcode a static message, but difficult for others to alter this message. A filter could be provided for other coders to be able to cleanly alter the default message, but this is beyond the skills of average users to implement.

    Another possibility is to add an option field somewhere that allows users to alter the default message. There are several possibilities, using a post to hold content is just as viable as any.

    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;
    
    ?>
    • This reply was modified 1 year, 1 month ago by  StephenEquus.

    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;
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Can we customize “the_content()”?’ is closed to new replies.