tags:

views:

806

answers:

4

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.

A: 

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:

function tidy_sidebar( $sidebar_name_or_id )
{
    ob_start();

    $bool = dynamic_sidebar( $sidebar_name_or_id);

    if ( $bool )
    {
     $str = ob_get_contents();
     $str = 'do cleanup stuff...';
    }
    else
    {
     $str = '';
    }
    ob_end_clean();

    return $str;
}

Then call echo tidy_sidebar( 'sidebar-name-or-id' ); from your theme.

Jeff
A: 

Also maybe check out the dynamic_sidebar_params filter --

Another link --

le dorfier
A: 

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:

<wordpress install directory>/wp-includes/default-filters.php
<wordpress install directory>/wp-includes/default-widgets.php

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.

pxl
A: 

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' 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:

function parse_html_widget_title( $text ) {
    return str_replace(array('[[', ']]'), array('<', '>'), $text);
}

Then I added a line below it to add the function as a filter:

add_filter('widget_title', 'parse_html_widget_title', 11); // 11 is one above the default priority of 10, meaning it will occur after any other default widget_title filters

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's widget_text for the Text widget output, or get_search_form for the search form [you can see it in wp-includes/general-template.php, at the get_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.

Andrew P