WordPress.org

Ready to get started?Download WordPress

Forums

Simple Facebook Connect + add_filter("the_content", "foo")=foo() called 2+ times (4 posts)

  1. TheInfinity
    Member
    Posted 3 years ago #

    Hi, everyone,

    at my wordpress installation with a custom plugin I have a really strange bug relating to simple facebook connect. My plugin uses
    add_filter('the_content','modify_content');
    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!

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 3 years ago #

    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.

  3. TheInfinity
    Member
    Posted 3 years ago #

    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?

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic