WordPress 101: Create a new widget area

Most WordPress themes come with a few useful widget areas where you can place any widgets you like. Sometimes, however, you need to add your own widget area because the theme you’re using doesn’t quite cut it. Paste this snippet in your theme’s functions.php file to create a new widget area:

function register_widget_area() {
    if( function_exists( 'register_sidebar' ) ) {
        register_sidebar( array(
            'name' => 'Widget Area Name',
            'id' => 'new_widget_area',
            'before_widget' => '<div class="widget">',
            'after_widget' => '</div>',
            'before_title' => '<h1 class="title">',
            'after_title' => '</h1>',
        ) );
    }
}
add_action( 'widgets_init', 'register_widget_area' );

After pasting that code the new area will become available in the Appearance > Widgets section of your dashboard. The options available in the snippet are pretty self-explanatory, so I won’t go through each of them – just rename the items to whatever your use case requires them to be.

Once you have added widgets to the new widget area, you can display them on your site easily by adding this line of code in the location that you want the widgets to appear:

dynamic_sidebar( 'new_widget_area' );

Tags: , , , , ,

No comments yet.

Leave a Reply