How to create a WordPress Widget

To create a wordpress widget is tantamount on saying that you would want to create a wordpress plugin. There would be differences though, but it goes without saying that you should learn how to create a plugin first.

I have covered the basics of that here in my tutorial series How to create WordPress plugins

so lets get going with the widget…

after you have created the file for your plugin/widget and the necessary details for it to be activated, add the following code…

function widget_content(){
$sdstr = “this is where your widget code goes.”;
return $sdstr;
}

function cc_widget($args) {
extract($args);
echo $before_widget;
echo $before_title;
echo “YOUR WIDGET TITLE”;
echo $after_title;
echo widget_content();
echo $after_widget;
}

function cc_widget_init()
{
register_sidebar_widget(__(‘YOUR WIDGET NAME’), ‘cc_widget’);
}

add_action(“plugins_loaded”, “cc_widget_init”);

activate your plugin/widget and add it to your sidebar throught the admin dashboard->appearance->widgets then of you go…


PHP, Wordpress

Leave a Reply