The correct practice is actually to use $args['before_widget]
and $args['after_widget']
to handle the id, class, and anything else. In pseudo code:
class My_Widget extends WP_Widget {
function My_Widget() {
//basic widget settings.
$widget_ops = array(
'classname' => 'my-name',
'description' => 'This is the description'
);
//widget control settings.
$control_ops = array(
'id_base' => 'my-name-widget'
);
//create widget
$this->WP_Widget( 'my-name-widget', 'Widget Title', $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
// split $args
extract( $args );
/* Our variables from the widget settings. */
$title = apply_filters( 'widget_title', $instance['title'] );
//apply any additional settings here
/* Before widget (defined by themes). */
echo $before_widget;
/* Display the widget title if one was input (before and after defined by themes). */
if( $title )
echo $before_title . $title . $after_title;
// do all the widget output
/* After widget (defined by themes). */
echo $after_widget;
}
}
If you’re still a bit confused, print_r()
$args
and you’ll see what all you have available to you. If you’re STILL confused, Justin Tadlock has a great guide that will get you going in the right direction.
It’s important that you use $before_widget
and $after_widget
because if someone were to register the widgets with <li>
s and you had hardcoded <divs>
, they would pretty quickly either become frustrated, or just move away from your code altogether.