I’m trying to filter ALL widget output through a simple filter, but can’t find any hooks and was hoping to be pointed in the right direction. Or possibly my efforts are not even possible?
My simple filter is something like this:
function clean_widget_output( $input ) {
return str_replace( array( "t", "n", "r" ), '', $input );
}
add_[FILTER OR ACTION]( 'need_a_hook', 'clean_widget_output', 99 );
Any ideas? I’m pretty new to PHP, but I can get around.
This was borne out of the need/desire to clean the god-awful HTML spewed by WordPress’ widgets. I love what they do, but some of the output makes me cry.
The short answer is
output buffering
because I couldn’t find any widget or sidebar hooks.The long answer is:
Then call
echo tidy_sidebar( 'sidebar-name-or-id' );
from your theme.I had a similar issue and after looking through Adam Brown’s list of all WordPress filter hooks, found that the hook I needed does exist (
widget_title
, as pxl mentions), but that there is no hook to get all widget output. I thought I’d elaborate on the solution that worked for me.Theoretically, the
widget_title
hook should affect all widgets on your blog, but I’m sure some 3rd party widgets neglect to include the necessary line of code to apply any title filters, so it’s not foolproof. It worked for me, however, and it can be used to apply custom ‘shortcode’ (more accurately, in this case, ‘longcode’) or syntaxes to your widget titles. For example, I wanted to occasionally include html code in my widget titles, but by default, all html is stripped out. So, in order to be able to add things like<em>
tags to text in some of my titles, I chose a custom syntax:[[
instead of<
&]]
instead of>
(for ex,[[em]]
and[[/em]]
) and then created a function in my theme’s functions.php file to process that custom syntax and replace it with the html equivalent:Then I added a line below it to add the function as a filter:
The
add_filter
/apply_filter
functionality automatically passes the content being filtered as the first parameter to the function specified as the filter, so that’s all you need to do.In order to do something similar for the main output of the widget, you would need to look at all your widgets to see what hook they use and verify that they have a filter for their main output, than use
add_filter()
for each hook you find with your custom callback function (for example, it’swidget_text
for the Text widget output, orget_search_form
for the search form [you can see it in wp-includes/general-template.php, at theget_search_form()
function]). The problem is that some of the dynamically generated widgets don’t have hooks (like the Meta widget), which is why the output buffering solution Jeff provides is the most versatile, though not ideal, solution.there are lots of hooks for wordpress widgets that aren’t documented. The wordpress codex doesn’t list them, for whichever reason (such as these hooks may change in the future and will break unexpectedly with new updates and versions)… so use these with extreme caution.
to find out what they are, there are at least 2 places to look:
contained in those two files is a pretty good listing of all the hooks wordpress uses.
An example would be a filter for widgets is
widget_title
again, use these with caution, they’re not guaranteed to work past the specific version of the code you’re looking at.
I’m not sure when they introduced the widget_text filter, maybe they didn’t have it in ’09 when this question was originally asked, but since it’s there now, and for the sake of anyone that gets this stackoverflow like I did from google and just happens to read far enough down to see this answer, it’s now actually quite simple:
Also maybe check out the dynamic_sidebar_params filter —
Another link —