Support » Plugins » Hacks » Simple Facebook Connect add_filter("the_content", "foo")=foo() called 2 times

  • Hi, everyone,

    at my wordpress installation with a custom plugin I have a really strange bug relating to simple facebook connect. My plugin uses
    for adding some custom content based on some keywords and replacers. It works fine – if I dont use SFC.

    If I do so modfy_content() is called several times – depending on the sub modules of SMF which are active. SFC base has no effect, SMF Like Button has one. The additional run of modify_content() is in my header just after my styles and short before the title tag. Its the place where SFC adds open graph meta tags if it needs them.

    Because modify_content sends mails, makes database entries etc its bad that its called several times. So my question is – how can I prevent this? Is it a bug of SFC? I don’t think so, because eg contact form 7 does not have this problem. So what did I wrong? And what has this to do with SFC?

    Thanks for any help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    You should never use the_content filter, or any filter for that matter, to take actual actions. Filters are intended to be filters of the content they’re being passed only. We have a difference between filters and actions for just that reason.

    And yes, as you have discovered, the_content filter can be called multiple times. SFC has to use it in several places because it needs to get the final content that will be displayed on the page. This means calling the_content filter to process shortcodes and things of that nature. This is not a bug, it is intentional, and it is by design.

    So if you use the_content filter to do anything other than filter the content, then you’re doing it wrong. Your modify_content should be capable of, and will be, called multiple times per page. Design your code accordingly.

    Hmm. Okay, understood – it’s logic and makes sense.

    What would be the right place to scan for shorttags and / or keywords in my content?

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    You can use the_content filter to scan for things, and that’s fine. You just shouldn’t take any non-repeatable action on those things from within the same function.

    For example, if you wanted to scan the content for keywords, then store the fact that those keywords are in there, that’s fine. But wait until some other action before sending an email or something.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Simple Facebook Connect add_filter("the_content", "foo")=foo() called 2 times’ is closed to new replies.