How to Register widgets in WordPress

Shortlink:

Hello, Today i will teach you how to register widgets in WordPress.

It’s simple work. just open your functions.php file in your current active theme folder, and input this code


function my_custom_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_widgets');

Now you can call / show display your content use below code

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

If you want to use with condition then you can use below codes

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

 

If you use above codes then if you have added any content in your WordPress dashboard widgets options then show it’s or if you don’t have added any content then don’t show widgets.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!




%d bloggers like this: