• Resolved vicchi

    (@vicchi)


    So I have a moderately successful plugin that appends or prepends an enhanced author biography to the content of a page/post/custom post type.

    It does this by hooking to either the_content or the_excerpt and appending/prepending content according to the plugin’s configuration.

    I’ve started getting support queries where the author biography is appearing in the sidebar of a site via a widget, for example via the Category Posts widget. The widget is using the_excerpt() within a custom query Loop, to pull posts according to a configured category and show the post excerpt within the context of the sidebar.

    As a direct effect of this, my plugin’s the_excerpt filter hook is being called. What I’d like to do is be able to detect whether my filter hook is being invoked within the context of the sidebar or a widget and conditionally decide whether to append my plugin’s content to the post content passed to the filter hook. The pseudo-code would look something like this …

    add_filter ('the_excerpt', array ($this, 'insert_biography_box'));
    
    function insert_biography_box ($content) {
        if (in_sidebar ()) {
            return $content;
        }
    
        // do code stuff to append/prepend biography content
        return $content;
    }

    … but after a lot of searching through the WordPress core source, on these forums and on the WordPress Answers Stack Exchange forum it doesn’t look like a function along the lines of is_sidebar or is_widget (or some other variation on the name) exists.

    Is it even possible to determine whether a filter hook function is being called within the context of the sidebar or within a widget?

    -Gary

Viewing 2 replies - 1 through 2 (of 2 total)
  • Have you explored the use of the in_the_loop function? I’ve never used it, but I could definitely see myself running into this problem someday… So after seeing your problem, I did a few google searches, I came across this function… http://codex.wordpress.org/Function_Reference/in_the_loop

    Perhaps something like this would work?

    function insert_biography_box ($content) {
        if ( !in_the_loop() ) {
            return $content;
        }
    
        // do code stuff to append/prepend biography content
        return $content;
    }

    I hope this helps!

    -greg

    Thread Starter vicchi

    (@vicchi)

    On a parallel thread over on StackExchange, someone suggested using is_main_query() and that works some of the time, with some plugins that use either the_content and/or the_excerpt, but not all of the time with all the plugins I’ve been testing against.

    Likewise, I’ve just tested using in_the_loop() and that works some of the time, with some plugins but not with all of them.

    But the magic combination of testing against is_main_query() and in_the_loop() seems to do the trick, so thank you so much for the heads up on this API call.

    So the (pseudo) code now looks something like this …

    add_filter ('the_excerpt', array ($this, 'insert_biography_box'));
    add_filter ('the_content', array ($this, 'insert_biography_box'));
    
    function insert_biography_box ($content) {
        if (!in_the_loop () || !is_main_query ()) {
            return $content;
        }
    
        // do code stuff to append/prepend biography content
        $biography = 'some-magic-function-return-value';
        return $content . $biography;
    }

    .. which now gives me precisely the behaviour I wanted, against as many plugins that use the content or excerpt filters in the sidebar and/or footer widgets.

    -Gary

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How To Determine If A Filter Is Called In A Sidebar/Widget Context?’ is closed to new replies.