Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do WordPress widget (or sidebar) hooks exist?

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.

like image 211
Jeff Avatar asked Sep 06 '09 15:09

Jeff


People also ask

What is widget and sidebar in WordPress?

A sidebar is any widgetized area of your theme. Widget areas are places in your theme where users can add their own widgets. You do not need to include a sidebar in your theme, but including a sidebar means users can add content to the widget areas through the Customizer or the Widgets Admin Panel.

Is WordPress a sidebar?

A WordPress sidebar is an area on a WordPress website alongside the main content, which displays extra information or a navigation menu. It's usually a vertical column on either side of a page, but it can also be found in other places, such as under the footer.

What is widget in WordPress?

What Are WordPress Widgets? A WordPress widget is a modular element that enables you to add a specific feature to your website. Widgets can be added to different areas of a website, such as a website's sidebars or footer areas, and they're an inherent part of WordPress' design and layout customizations.


2 Answers

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.

like image 180
Jeff Avatar answered Oct 06 '22 13:10

Jeff


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:

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.

like image 31
Andrew Patton Avatar answered Oct 06 '22 14:10

Andrew Patton