user1384705
user1384705

Reputation:

How to automatically wrap WordPress widgets in a div?

How do I automatically wrap default WordPress widgets into a DIV without altering the widgets themselves? I'm trying to do this, automatically:

<div class="widget">
Some WordPress widget's code
</div>

Is there a way to automatically wrap these widgets?

Upvotes: 1

Views: 3766

Answers (2)

Mustaasam Saleem
Mustaasam Saleem

Reputation: 166

You can add html directly when you're registering a widget. You can see, how i added html in our widget.

Like below:

<div id="sample-widget" style="text-align:center">

Below is the complete code for registering a widget.

// Registering new widget area
function sample_widgets_init() {
    register_sidebar( array(
        'name' => 'Footer',
        'id' => 'Footer',
        'before_widget' => '<div id="sample-widget" style="text-align:center">',
        'after_widget' => '</div>',
        'before_title' => '<h2 class="rounded">',
        'after_title' => '</h2>',
    ) );
}
// Initializing our newly created function named as "sample_widgets_init"
add_action( 'widgets_init', 'sample_widgets_init' );

Upvotes: 3

Clark T.
Clark T.

Reputation: 1470

that is handled when you register the sidebar see http://codex.wordpress.org/Function_Reference/register_sidebar

and example as well

register_sidebar( array(
    'name' => __( 'Main Sidebar', 'twentyeleven' ),
    'id' => 'sidebar-1',
    'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    'after_widget' => "</aside>",
    'before_title' => '<h3 class="widget-title">',
    'after_title' => '</h3>',
) );

Upvotes: 3

Related Questions