How to create custom widget in WordPress ?

How to create custom widget in WordPress ?

Hi guy’s today i am gonna tell you how to create custom widget in WordPress. Widget is a very important part when you are building website in WordPress. Basically WordPress already have some widgets but when we are building the site we needs the more widgets to use in the site so follow these instructions to create widget in WordPress.



Before moving on let me tell you one thing that Please do not create new function for widget it has already present there you just have to add some code within the function and thats it. See the below instructions.

Step 1

Please put these code in your function.php file within this function.

function themename_widget_init()
{
your widget code goes here ..
}

Your widget code will be

register_sidebar( array(
    'name' => __( 'Widget Name', 'themename' ),
    'id' => 'widget_id',
    'description' => __( 'widget description.', 'themename' ),
    'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    'after_widget' => '</aside>',
    'before_title' => '<h3 class="widget-title">',
    'after_title' => '</h3>',
) );

and your done now go to WordPress backend go to widget under appearance you can see the new widget name “Widget Name” you can change name whatever you wish to.

Please see this screenshot

widget

Now you can display this widget anywhere by using this function

<?php dynamic_sidebar('widget-id'); ?>

Hope you like this tutorial if you have any question or comments please fill the form below. We would love to help you anytime.
Good Luck !

Leave a Reply

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