For registering a widget use this piece of code in your functions.php.
Remember you must call it with a widgets_init action hook.
function site_widgets_init() {
register_sidebar(
array(
'name' => esc_html__( 'widget name', 'text-domain' ),
'id' => 'widget-name',
'description' => esc_html__( 'Add widgets here.', 'text-domain' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
)
);
}
add_action( 'widgets_init', 'site_widgets_init' );
