How to add / register a new sidebar in WordPress ?
Building a WordPress theme is incomplete without adding a sidebar in most cases or you might want to add it in your theme for your custom requirement.So in order to build ,we need to use the pre built function of register_sidebar() function located in wp-includes/widgets.php of your WordPress installation which uses widgets_init hook when added into code.
Register a Sidebar in WordPress
Follow the steps
- Open functions.php of your WordPress theme either from Appearance/editpr and select Theme Functions(functions.php) from left side or open it from ftp
register_sidebar( array( 'name' => __('Custom sidebar', 'custom_sidebar_name'), 'id' => 'id_you_want_to_set_here', 'before_widget' => '<div class="class_you_want_to_set_here">', 'after_widget' => '</div>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>' ));
- Copy and paste the above code into functions.php
- Now you can access WordPress new sidebar from Appearance->widgets sections from WordPress dashboard
Explaining parameters of the function register_sidebar()
- Replace 'id_you_want_to_set_here' with your specific custom id which assigns id to the sidebar
- 'before_widget' is the element which contains the code block which appears at top of each and every widget of the sidebar ,so replace the 'class_you_want_to_set_here' with your class
- 'after_widget' is the element which contains the code block which appears at end of each and every widget of the sidebar
- 'before_title' is the element which contains the code block which sets the widgets title enclosed in it so replace the 'widgettitle' with your class if you want
- 'after_title' is the element which contains the code block which sets the widgets title enclosed in it