Forums

Unwanted behaviour: add_filter also processes widget contents! (2 posts)

  1. markowe
    Member
    Posted 1 year ago #

    I am filtering the_content to add some stuff to page/post content before displaying it. Problem is, widgets that display content excerpts (like Recent Posts, for example) are ALSO getting filtered and my content added on, which is not desirable. Seems simple, but I can't seem to work around that. Should I be filtering something else, and not the_content, or is there some way to check if we are in a widget and not filter the content, like is_widget (doesn't exist that I know of)?

    Thanks.

  2. vtxyzzy
    Member
    Posted 1 year ago #

    In your filter, you can add a check for a global variable that you can turn on and off:

    function my_filter ($content) {
       global $my_global_content_switch;
       if ( !$my_global_content_switch ) return $content;
       // rest of filter code
    }

    Then, in the template:

    $my_global_content_switch = 1;
    the_content();
    $my_global_content_switch = 0;

Topic Closed

This topic has been closed to new replies.

About this Topic