Register sidebar

You can register sidebar by calling register_sidebar() method. Just provide a unique name to each sidebar (eg: “Right Sidebar”, “Left Sidebar”). These names are displayed in WordPress admin dashboard interface.

function wpb_widgets_init() {
register_sidebar( array(
		'name' => __( 'My Sidebar', 'wpb' ),
		'id' => 'my-custom-sidebar',
		'description' => __( 'The main sidebar appears on the right on each page except the front page template', 'wpb' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h4 class="widget-title">',
		'after_title' => '</h4>',
	) );
}
add_action( 'widgets_init', 'wpb_widgets_init' );

Display sidebar

To display sidebar in your theme, you can use dynamic_sidebar() method by passing the sidebar id.

<?php if ( is_active_sidebar( 'my-custom-sidebar' ) ) : ?>
	<aside id="sidebar" class="sidebar right">
		<div id="sidebar" class="widget-area" role="complementary">
			<?php dynamic_sidebar( 'my-custom-sidebar' ); ?>
		</div>
	</aside>
<?php endif; ?>
Tagged in : PHP WordPress

Nilanchala

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.