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

  1. 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
  2. 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>'
    ));
    
  3. Copy and paste the above code into functions.php
  4. Now you can access WordPress new sidebar from Appearance->widgets sections from WordPress dashboard

Explaining parameters of the function register_sidebar()

  1. Replace 'id_you_want_to_set_here' with your specific custom id which assigns id to the sidebar
  2. '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
  3. 'after_widget' is the element which contains the code block which appears at end of each and every widget of the sidebar
  4. '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
  5. 'after_title' is the element which contains the code block which sets the widgets title enclosed in it

register,sidebar,wordpress