views:

132

answers:

2

I am writing a Wordpress plugin that does string processing whenever 'the_author' filter event is fired. However, some widgets also contain 'the_author' event and subsequently my plugin is fired which should not happen. So I am trying to detect if my plugin is called from certain widgets but so far to no avail. One widget that I would like to ignore is called 'Recent Comments'. I have tried:

function wrap_author($the_author) {
    if(!is_active_widget('recent_comments')) {
        $the_author = '<span class="CA_author">' . $the_author . '</span>';
        return $the_author;
    }
}

It could be that I am not using the right name for the widget, I have Googled a lot to find the proper internal name for the Recent Comments Widget but can't find it so far. Or maybe I should not be using is_active_widget function.

Your suggestions are much appreciated.

A: 

I'm not sure there's any way to achieve this. The is_active_widget function will only tell you if a widget is displayed on the site (and if so which sidebar it's in), not if it's currently being processed.

The Widget Logic plugin code suggests a possible solution. You could replace the callback function for each widget with a function in your plug-in, and store the original callback function name in a new variable. Then when your function is called remove your filter, call the callback original function and then re-enable your filter (alternatively you could perhaps set a flag to indicate a widget is being processed). Check out the last two functions in the Widget Logic plugin code…

Richard M
Dear Richard,Thanks a lot for your suggestion, it's definitely a method that makes sense. However, I want to distribute my plugin and this would require the installation of an additional widget which is too cumbersome for most Wordpress users. So I hope there is still a solution available using the Wordpress core code.
DrDee
What I suggested doesn't involve users installing the Widget Logic plugin, but rather you using some code from that plugin as the basis for part of yours.
Richard M
ooppsss.. thanks for the clarification!
DrDee
+1  A: 

If you just want yours to fire on the "main" content areas then use the in_the_loop() function to check and see if you're in a content loop. This will probably get you 99% of the way there but you'll almost certainly find some edge case that'll cause frustration ;)

if (in_the_loop()) {
// do stuff
}

This will keep your code from executing at all in a sidebar.

Gipetto
this is actually a quite nice solution, I think this will do the job, thanks!
DrDee