Register widget in wordpress

function my_custom_theme_widgets() {
	register_sidebar( array(
		'name' => 'My Widget',
		'id' => 'widget_id',
		'before_widget' => '<div class="widget_div">',
		'after_widget' => '</div>',
		'before_title' => '<h2>',
		'after_title' => '</h2>',
	) );
}
add_action('widgets_init', 'my_custom_theme_widgets');

You can call widget via below:

Single widget:

<?php dynamic_sidebar('widget_id'); ?>

Conditional

<?php if ( ! dynamic_sidebar( 'sidebar-top' ) ) : ?>
// Your conditional codes
<?php endif; ?>

3 thoughts on “Register widget in wordpress”

Leave a Reply