You are currently viewing How to Create a WordPress Widget in a Plugin

How to Create a WordPress Widget in a Plugin

Today  I will discuss with you that how you can create a WordPress widget in a plugin.

First of all, you should create a file anything you like to call it, as I made a “mywidgetp.php“, in this file you will write your code.

this is a simple widget plugin, so here is only 1 file needed.

Write these lines at the start of the plugin

<?Php /* Plugin Name: Hello World Plugin URI: https://www.tdevelopers.com/ Description: Sample Hello World Plugin Author: Developer Version: 1 Author URI: https://www.tdevelopers.com/ */

These are the few lines that WordPress understands that it’s the plugin, if you do not write these lines the plugin will not activate as WordPress never understand it.

function sampleHelloWorld() { echo "

Hello World";
}




function widget_myHelloWorld($args) {
extract($args);
echo $before_widget;
echo $before_title;?>My Widget Title< ?php echo $after_title; sampleHelloWorld(); echo $after_widget; } function myHelloWorld_init() { register_sidebar_widget(__('Hello World'), 'widget_myHelloWorld'); } add_action("plugins_loaded", "myHelloWorld_init"); ?>

These are the functions that I used in my creation, it is so simple to get started and understand the widget plugin.
in the last function register_sidebar .., it is used to register your widgets, you can see it contains the title of your widget and that function which you created above.
and last action add_action is the WordPress hook that is loaded when you activate the plugin, you can see that it contains the last function in which you register the sidebar.
So that’s it, hope you learn something from it.