Create your own custom widget

The WordPress Widget makes it so easy for WordPress users that it can be used anywhere on the site with just a drag and drop. There are many WordPress themes or plugins on the market that allow widget users to create their own layouts. Plugins are even available for widget management. Today’s post will show you how to create a custom WordPress widget for yourself.

What is a WordPress widget?

WordPress Widget is basically designed to help its users to design and structure the site in a simple way. The most interesting thing about using WordPress widget is that you can use it in any sidebar or widget ready area by dragging and dropping on your site. This is a very useful tool for WordPress theme developers or plugin developers which multiplies the speed of their work. They create functionality for the site through various programming and the user can use them where they need to customize their site according to their needs. This is possible only because there are WordPress widgets. You can also create your own custom WordPress widget and use it as a drag and drop on your site.

Creating custom WordPress widgets

You need to paste the code given below in the functions.php file of the theme. The code that is kept for you here will greet the visitors.


// Widget description
array( ‘description’ => __( ‘Sample widget based on WPBeginner Tutorial’, ‘tortoizthemes_widget_domain’ ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( ‘widget_title’, $instance[‘title’] );
// before and after widget arguments are defined by themes
echo $args[‘before_widget’];
if ( ! empty( $title ) )
echo $args[‘before_title’] . $title . $args[‘after_title’];
// This is where you run the code and display the output
echo __( ‘Hello, World!’, ‘tortoizthemes_widget_domain’ );
echo $args[‘after_widget’];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ ‘title’ ] ) ) {
$title = $instance[ ‘title’ ];
}
else {
$title = __( ‘New title’, ‘tortoizthemes_widget_domain’ );
}
// Widget admin form
?>


get_field_id( ‘title’ ); ?>” name=”” type=”text” value=”” />

}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance[‘title’] = ( ! empty( $new_instance[‘title’] ) ) ? strip_tags( $new_instance[‘title’] ) : ”;
return $instance;
}
} // Class tortoizthemes_widget ends here
// Register and load the widget
function tortoizthemes_load_widget() {
register_widget( ‘tortoizthemes_widget’ );
}
add_action( ‘widgets_init’, ‘tortoizthemes_load_widget’ );

Now go to your dashboard Appearance »Widgets, drag and drop the Techonlinebd widget to any of your sidebar areas. Visit the site to see what you have done for your site.

Isn’t that a very common job? I hope the post will be useful to you, if you have created a widget through it, please comment and request to give the link. Stay well.