Register Widget in WordPress -10
Widgets are blocks of content that you can add to your site’s sidebars, footers, and other areas of your website where you want. These are created to provide a simple, easy and user friendly to control the design and content of their site without doing code. register_sidebar function is used to register a widget with any name and add_action use widgets_init hook to register the widget with specific name.
//widgets_init function to insert a widget
function sidebar()
{
register_sidebar(array(
‘id’ => ‘SidebarforBlog’,
‘name’ => __(‘SidebarforBlog’),
‘before_widget’ => ‘<div id=”%1$s” class=”widget %2$s mb-3″>’,
‘after_widget’ => ‘</div></div>’,
‘before_title’ => ‘<h3 class=”widget-title font-weight-bold” style=”font-size:20px;”>’,
‘after_title’ => ‘</h3><div class=”card p-2″> ‘,
)
);
}
add_action(‘widgets_init’, ‘sidebar’);
Steps to add content in widgets
Step 1: Open admin panel
- Go to this page: http://localhost/[project-name]/wp-admin/
Step 2: Click on Appearance → Widgets
- Here you see the widgets ‘SidebarforBlog’.
- Now you can drag and drop any widget from the available widgets
- Then Save it.
Step 3: Call the widget
<?php dynamic_sidebar(‘SidebarforBlog’);?>