Register Widget Area in WordPress

Register Widget Area in WordPress

13 / 100 SEO Score

If you work in WordPress and need a way to register a new widget area in WordPress where you can use plugin to do that or add simply code on WordPress function.php file.

This code snippet will let you register widget area using WordPress built-in register_sidebar function

Steps to register widget area on WordPress:

  1. Open WordPress admin panel then go to Appearance > Theme Editor
  2. Open theme functions.php file, if you work localhost open the same file using any code editor IDE
  3. Add the following code at the bottom of the file.
  4. Save the changes and add display sidebar code where the page you need.
function zytheme_widgets_init(){
    $widget_before = '<aside id="%1$s" class="widget %2$s">';
    $widget_after = '</aside>';
    $title_before = '<h4 class="widget-title">';
    $title_after = '</h4>';

    register_sidebar(array(
        'name' => esc_html__('Side Area', 'astred'),
        'id' => 'sidearea',
        'description' => esc_html__('Display widgets on sidearea when sidearea option in theme is active.', 'astred'),
        'before_widget' => $widget_before,
        'after_widget' => $widget_after,
        'before_title' => $title_before,
        'after_title' => $title_after,
    ));
}

add_action('widgets_init', 'zytheme_widgets_init');

To display widget area where you need like sidebar adds the following code:

if ( is_active_sidebar( 'sidearea' ) ) :
  echo '<div id="sidearea" class="widget-area" role="complementary">';
    dynamic_sidebar( 'sidearea' );
  echo '</div>';
endif;

Leave a Reply

Your email address will not be published. Required fields are marked *