How to Create a WordPress Widget in a Plugin

Share

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 siple widget plugin, so here is only 1 file needed.

Write these lines in start of the plugin

[code lang=”php”]

< ?php /* Plugin Name: Hello World Plugin URI: http://www.tdevelopers.com/ Description: Sample Hello World Plugin Author: Developer Version: 1 Author URI: http://www.tdevelopers.com/ */ [/code] These are the few lines that the WordPress understand that its the plugin, if you not write these lines the plugin will not activate as wordpress never understand it. [code lang="php"] 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"); ?>
[/code]

These are the functions that i used in my creation, its so simple to get started and understand the widget plugin.
in the last function register_sidebar .. , its 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.

© 2018 IT Tech Developer | XML Sitemap | HTML Sitemap
Powered by T-Developers | Designed by: Shah Rukh | Thanks to Friends
show
 
close
rss Follow on Twitter facebook linkedin youtube google+ email