Hooking Into Widget Output Loop

When wordpress sidebar outputs any particular registered sidebar it loop through all widgets that assigned through it and outputs it (i guess). Is is possible to hook into the loop and add some content, say I want to add a ad code every three widgets.

What I have tried? Unable to find any leads. Tried to search on wordpress source code but not sure what to search. searching widget or widget loop doesn’t help.

2 Answers
2

Hook into 'dynamic_sidebar' and count how often it is called.

You get the current active sidebar with key( $GLOBALS['wp_registered_sidebars'] ).

add_action( 'dynamic_sidebar', 'wpse_96681_hr' );

function wpse_96681_hr( $widget )
{
    static $counter = 0;

    // right sidebar in Twenty Ten. Adjust to your needs.
    if ( 'primary-widget-area' !== key( $GLOBALS['wp_registered_sidebars'] ) )
        return;

    if ( 0 !== $counter && 0 === $counter % 3 )
        print '<hr>';

    $counter += 1;
}

Leave a Comment