[wp-hackers] Sidebar widget HTML/DOM

ravi ravi-lists at g8o.net
Thu Jun 24 14:38:37 UTC 2010


On Jun 23, 2010, at 8:22 PM, [ ravi ] wrote:
> 
> right now, register_sidebar() provides a few options to make additions to the HTML in widgets through the args ‘before_widget’, ‘after_widget’, ‘before_title’, ‘after_title’. However, this does not give me a good way to style the contents of the widgets (in a theme). One possibility is to use ‘after_title’ and ‘after_widget’ to wrap the content in a DIV, but that’s not always reliable, since some widgets do not have titles.
> 
> Currently I have a hack in jQuery to handle the special case of non-titled widgets, but its a hack and I am not happy with it. I am poring over the action/filter hooks to see if I can use one of them to modify widget HTML. But I figured I would ask the WP gurus here: any suggestions on how to go about this business?
> 

FYI, the jQuery hack I have is to first use ‘before_title’ to add an <h2> element with class=‘widgettitle’ to each element that has a title. This gives me something like this in the sidebar (using the Pages widget as an example):

<div class=‘widget ….’ ….>
	<h2 class=‘widgettitle’ ….>Pages</h2>
	<ul>
		<li> … page1 … </li>
		<li> … page2 … </li>
	</ul>
</div>

Since I want to wrap the UL (in this example) in a DIV so I can style it (make it collapsible, actually, in my case), here’s what I do in jQuery:

   jQuery('.widgettitle').each(
       function()
       {
           jQuery(this).nextAll().wrapAll('<div class="widgetcontent" />’);
       }
   );

This works for me, for now, but it would still be nice to be able to wrap it explicitly/deterministically in PHP, so if anyone has any ideas, including filters/actions that would accomplish this on the server end, please do let me know.

	—ravi
                                                                                                                 


More information about the wp-hackers mailing list