Create a Widget Area in WordPress

Home/Create a Widget Area in WordPress

Today’s WordPress themes are becoming more and more flexible where web design is concerned, but I’m still asked from time to time to create custom widget areas for clients.   Fortunately, the process is a fairly simple one and you can create a simple widget area via your functions.php file.

Simply add the following code to your functions.php file of your theme.

Register your new Widget Area

/** Register Widget Area. **/
function mycustom_widgets_init() {
 
 register_sidebar( array(
 'name' => 'My Custom Sidebar',
 'id' => 'my_custom_sidebar',
 'before_widget' => '<div>',
 'after_widget' => '</div>',
 'before_title' => '<h2 class="rounded">',
 'after_title' => '</h2>',
 ) );
}
add_action( 'widgets_init', 'mycustom_widgets_init' );

Your new widget area will now be available in the Appearance > Widgets area of your WordPress Dashboard.

Display your new Widget Area in your theme

Once your widget area has been registered, there’s still the matter of displaying it within your theme. Again, this is a fairly simple process. Add the following code to your functions.php file after the code above.

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('my_custom_sidebar') ) :
 
endif; ?>
2017-05-19T15:48:04+00:00 August 31st, 2015|WordPress Snippets|1 Comment

About the Author:

Warren Chandler is a freelance web programmer and web developer, based in Frinton on Sea, Essex. Warren specialises in web programming, web design, corporate identity, copy writing, SEO, logo and print media for home, work, and mobile platforms.

One Comment

  1. Dejan April 5, 2017 at 4:22 pm - Reply

    awesome ty, you really know your stuff 🙂 🙂 i have been trying this for ages and this helped me get it done in 2 minutes. cheers

Leave A Comment