Problems with the sidebar args and wp_list_bookmarks

I have run into an issue trying to use the sidebar args (before_widget,after widget,after_widget,before_widget) as theme style friendly parameters for the function wp_list_bookmarks, that is that the wp_list_bookmarks output will look just as the one
inside WP_Widget_Links.

I almost copied verbatim the code of the links widget but when trying to reuse the only one sidebar args (via
the global $wp_registered_sidebars) I get some css classes for the widget container that dont show up
using the WP_Widget_Links.

Read More

For example the following dump of a sidebar args:

'name' => 'Sidebar 1'
'id' => 'sidebar-1'
'before_widget' => '<li id="%1$s" class="widget %2$s">'
'after_widget' => '</li>'  
'before_title' => '<h2 class="widgettitle">'
'after_title' => '</h2>'

By reading the wp code I assumed that is safe to change %1$s, %2$s for %id, %class resp, and then once the
wp_list_bookmarks gets executed I should end with before_widget looking like <li id="linkcat-" class="widget linkcat">.
But after comparing to the output of the links widget the widget css classname is missing, and the output of my code looks
a bit odd in the page.

This is the code where I use the function, which gets called as a filter to sidebars_widgets:

function my_sidebars_widgets($sidebar_widgets) {
    global $wpdb, $wp_registered_sidebars;
    static $hits;
    if(++$hits == 2) {

        $sidebar = array_values($wp_registered_sidebars);

        $before_title = $sidebar[0]['before_title'];
        $after_title = $sidebar[0]['after_title'];
        $before_widget = $sidebar[0]['before_widget'];
        $after_widget = $sidebar[0]['after_widget'];

        $before_widget = str_replace('%2$s','%class',$before_widget);
        $before_widget = str_replace('%1$s','%id',$before_widget);

        wp_list_bookmarks(apply_filters('widget_links_args',array(
            'title_before' => $before_title, 'title_after' => $after_title,
            'category_before' => $before_widget, 'category_after' => $after_widget,
            'hide_invisible' => true,'title_li' => 'Links',
            'categorize' => 0,'inquiry' => true
        )));
    }
    return $sidebar_widgets;
}

Did you have any clue of what Im missing here? I keep trying to get clear how WP feed widgets args but
no explanation so far while reading the code.

thanks in advance for any on your considerations.

Related posts

Leave a Reply

1 comment

  1. After a few var_dumps I know this:

    1) When wp_list_bookmarks is called whithin WP_Widget_Links->widget
    The 'class' parameter is ignored because the 'category_before' parameter
    doesn’t have the %class wildcard. Instead 'category_before' will
    look as if ‘class’ where 'widget_links'.

    This is how the function dynamic_sidebar set the $before_widget parameters
    for the widget once it gets called.

    Of course this can be altered by filters like 'widget_links_args' or 'dynamic_sidebar_params'.

    2) On many themes in order to present default widgets the wp_list_bookmarks is called
    but with zero parameters then the default values are used as defined in bookmark-template.php:

    ('class' => 'linkcat','category_before' => '<li id="%id" class="%class">')
    

    and this happen even when the sidebar is registered using:

    ('before_widget' => '<li id="%1$s" class="widget %2$s">')
    

    which can let to a little inconsistence because WP_Widget_Links
    'category_before' parameter is initially borrow from sidebar 'before_widget'
    parameter.