0

I have a plugin I created as a test and am running into a bit of trouble. The code is executing 4 times and placing what I echo in the <head> tag of the page. The reason I'm performing an echo is that I am having trouble with another plugin and cannot figure out why my code was being executed 4 times. So, I created a simple example that I can replicate and hopefully fix (with your help). Now onto the code...

My functions.php:

/*
Plugin Name: What Have I Done?
Plugin URI: http://www.myplugintest4times.com
Description: This is just a test for replicated html in the head.
Version: 1.0.0
Author: Someone Special
Author URI: http://www.somewhereoutthere.com
 */

// Definitions
define('MY_PLUGIN_PATH' , plugin_dir_path(  __FILE__  ));
define('MY_PLUGIN_URL', get_option('siteurl') . '/wp-content/plugins/testplugin');

// Classes
require_once(dirname(__FILE__) . '/includes.php');

My includes.php:

function mytest_filter($content) {
    global $post;

    echo "<p>my test again</p>";

    $testpage = get_page_by_title('Test');
    $subtestpage = get_page_by_title('SubTest');

    if ($post->ID == $testpage->ID || wp_get_post_parent_id($subtestpage) == $testpage->ID) { // this is a test page, filter the content...
        switch($post->ID) {
            case $testpage->ID:
                echo "this is the test page"; // return $html;
                break;
            case $subtestpage->ID:
                echo "this is the sub test page (test is the parent)"; // return $html;
                break;
        }
      }
      else {
            return $content;
      }
} 

add_filter('the_content', 'mytest_filter');

Now, I have two pages on the site, one called Test and one called SubTest. When either of those pages are the current $post object, I am outputting my content instead of WordPress's $content. I've been doing this for almost 3 years now, so if I've been doing it wrong all these years, I need some direction.

The resulting html is:

<p>my test again</p>
this is the test page
<p>my test again</p>
this is the test page
<p>my test again</p>
this is the test page
<p>my test again</p>
this is the test page

And when viewing source for the page, this is all in the <head>. It also appends the content properly in the <body> tag, but the other is there and is causing headaches because when I peform a POST, it gets executed 4 times. So, is this not the proper way to inject/replace my content on a page?

1 Answer 1

2

Filters should return, not echo.

function my_content( $content ) {
    // Something something
    $content = 'my content';
    return $content;
}
add_filter( 'the_content', 'my_content' );

You can echo, but you'll need output buffering, like this:

function my_content( $content ) {
    // Something something
    ob_start();

    echo 'my content';

    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}
add_filter( 'the_content', 'my_content' );

Also, the reason you're seeing your output in the head section is because the filter is likely being used somewhere in the head section, perhaps by some plugin, so be careful since you might be overriding something more than you're hoping to.

Hope that helps.

3
  • Thanks for the response @kovshenin. As you can see from my example, I have return $html; commented out as I was just testing the issue. I knew the echo shouldn't go there, but wanted to see what was being fired 4 times. The output buffering is a new idea to me. Thanks for the tip. I'll try to implement that. Will this also work on a POST? The issue was actually discovered when I was performing a POST in a <form> and noticed that my insert statements in the db were happening 4 times.
    – clockwiseq
    Commented Apr 8, 2015 at 23:45
  • If your code is executed four times, it means the the_content filter is applied four times. Do you still get 4x with all plugins deactivated?
    – kovshenin
    Commented Apr 9, 2015 at 7:45
  • That's exactly what it was. So for each plugin that adds that particular filter, WordPress executes that filter and concatenates them (so to speak) I guess. I did disable other plugins and the number of times it was repeated was reduced. So, I have decided to go another route to correct my issues and perform my POST functionality in with get_header(). @kovshenin, thanks so much for the help!
    – clockwiseq
    Commented Apr 9, 2015 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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