2

I was trying to add a content filter and i discovered that the filter was being called 3 times in total. I tried slowly deleting all of the plugins and eventually found that it was Yoast that was causing this. This is absolutely insane. This means that ever single content filter gets called 3 times every single time? The performance issues alone are seriously bad. Any idea what is causing this to happen. Surely this plugin would not be released if it was calling the content 2 times.

For example. This hook will display one in the content (it does this without the plugin installed) and 2 more times at the top of the page when Yoast SEO is installed.

add_action( 'the_content', 'outputsomething');
function outputsomething() 
{
    echo "test";
}
  • You could print a call-stack instead, to see where inside Yoast this is happening? I assume you're using the latest Yoast SEO version, and free not paid? – Rup Apr 25 '16 at 18:07
  • yes free version of yoast, up to date and unaltered. Did the stack trace but the thing is huge, i cant pull anything useful from it – user1889580 Apr 25 '16 at 18:55
  • I've tried reproing this and can't sorry: I only get the one echo. Maybe it's some settings inside Yoast that you've turned on? Or maybe running Yoast somehow triggers your add_action to add the same action three times. – Rup Apr 26 '16 at 9:57
  • I cannot comment as I lack reputation but I can confirm that this is indeed a problem. I have a counter inside the callback function for add_filter('the_content, 'some_function') and instead of the counter being incremented once, it's incremented 3 times! I've drilled the problem to be Yoast as it only happens when my plugin and Yoast are active at the same time. – Paolo Oct 26 '16 at 7:52
  • Added an issue thread here => github.com/Yoast/wordpress-seo/issues/5956 – Paolo Oct 26 '16 at 8:06
2

The core of your problem is that the_content is a filter not an action, and it is supposed to return values, not echo them.

As for the "called number of times" part of the question, yes any hook can be called unlimited number of time. If you find that you need to return one result on the first call and different on the others it is a sign that you are doing something wrong or extremely hacky.

| improve this answer | |
1

Not sure what your question exactly is but yeah - for every apply_filters( 'the_content', '...' ); all hooked filters will be executed. I wouldn't care that much how many times this happens but instead what content is being filtered each time - more the content bigger the performance penalty.

| improve this answer | |
  • 1
    This is not true as I have several other content hooks in other plugins and they are not causing all hooks to be called again. Surely when you call the_content it only calls the function that I'd applied to that filter – user1889580 Apr 25 '16 at 18:43
0

I'm afraid I cannot reproduce this problem. Running WP 4.6.1 and Yoast 3.7.1. I added the following to the functions file of my test install:

add_action( 'the_content', 'wpse224783_action');
function wpse224783_action() 
    { echo "test123"; }

add_filter( 'the_content', 'wpse224783_filter');
function wpse224783_filter($content) 
    { echo "test456" ; return $content . "test789" }

This leads to all my contents outputting test123test456test789. So, my conclusion is that this behaviour is not triggered only by the combination of a fresh WP install and Yoast. I used a profiling plugin, which indicated the functions only running once for every call to the_content. Somewhere else, possibly in your theme, something is triggering the_content multiple times.

| improve this answer | |

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.