How to add and define a widget area in WordPress?

How to add and define a widget area in WordPress?

In your functions.php, simply add the following code:


function THEMENAME_widgets_init() {      
register_sidebar( array(           
    'name' => __( 'My Widget Area', 'THEMENAME' ),           
    'id' => 'sidebar-1',           
    'description' => __( 'Add widgets here to appear in your sidebar.', 'THEMENAME' ),           
    'before_widget' => '

',           
    'before_title' => '

',           
    'after_title' => '

',      
) );
}
add_action( 'widgets_init', 'THEMENAME_widgets_init' );
?>

Note:
1. THEMENAME is user-defined, you may change this to your actual theme folder name.
2. You need to add a separate code in your HTML structure (e.g. page.php file) to call out the defined widget area.

To print your widget area to your theme file, add the following code:


Note: sidebar-1 is the class name in the functions.php definition.