register widget in wordpress code example

Example 1: custom widget area wordpress

<?php
  
 /**
 * Register our sidebars and widgetized areas. - Most times you can place this instead your fuction file.
 *
 */
function arphabet_widgets_init() {

	register_sidebar( array(
		'name'          => 'Home right sidebar',
		'id'            => 'home_right_1',
		'before_widget' => '<div>',
		'after_widget'  => '</div>',
		'before_title'  => '<h2 class="rounded">',
		'after_title'   => '</h2>',
	) );

}
add_action( 'widgets_init', 'arphabet_widgets_init' );
?>
  
/* show sidebar - template file */  
<?php if ( is_active_sidebar( 'home_right_1' ) ) : ?>
	<div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
		<?php dynamic_sidebar( 'home_right_1' ); ?>
	</div><!-- #primary-sidebar -->
<?php endif; ?>

Example 2: register sidebar wordpress

<?php if ( is_active_sidebar( 'your-sidebar-slug' ) ) { ?>
    <ul id="sidebar">
        <?php dynamic_sidebar('your-sidebar-slug'); ?>
    </ul>
<?php } ?>

Example 3: how create widget widget in wordpress

/*  add this code in footer.php child theme file  */

		<div id="footer-sidebar" class="secondary">
			<div id="footer-sidebar1">
				<?php
				if(is_active_sidebar('footer-sidebar-1')){
				dynamic_sidebar('sidebar1');
				}
				?>
			</div>
			<div id="footer-sidebar2">
				<?php
				if(is_active_sidebar('footer-sidebar-2')){
				dynamic_sidebar('sidebar2');
				}
				?>
			</div>			
		</div>
        
/*  add this code in functions.php child theme file */
    
    register_sidebar( array(
	'name' => 'Footer #1',
	'id' => 'sidebar1',
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
	) );
	register_sidebar( array(
	'name' => 'Footer #2',
	'id' => 'sidebar2',
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
	) );
    
    }
add_action( 'wp_enqueue_scripts', 'twentytwenty_child_enqueue_child_styles' );

Example 4: register sidebar wordpress

/**
 * Add a sidebar.
 */
function wpdocs_theme_slug_widgets_init() {
    register_sidebar( array(
        'name'          => __( 'Main Sidebar', 'textdomain' ),
        'id'            => 'sidebar-1',
        'description'   => __( 'Widgets in this area will be shown on all posts and pages.', 'textdomain' ),
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget'  => '</li>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>',
    ) );
}

Tags:

Php Example