I was asked to convert a Wordpress plugin of mine to a Wordpress widget. It turned out to be a simple process but simple directions were lacking.
You can find examples of widgets as they are meant to be written at the end of the /wordpress/wp-includes/widgets.php file. Use those examples as your template should you have any questions.
Below is a skeleton of a WP Widget. All necessary parts are in this file, all unnecessary ones are not. Start with this and just plunk your plugin code in the section indicated in the comments.
<?php/*
Plugin Name: Your Plugin/Widget Name goes here
Version: 1.0
Plugin URI: The plugin webpage for help and more information
Author: Your name
Author URI: Your homepage
Description: Description of widget
*/
// all functions go in this function this is your main
function your_very_unique_widget_name_badge_init() {
//gracefully fail if sidebar gets deactivated
if ( !function_exists(’register_sidebar_widget’) )
return;
//the code specific to your widget goes in this function
function your_very_unique_widget_name_badge($args)
{ //fetch theme related things
extract($args); // this information is from what the user input in our control function below
$options = get_option ( ‘your_very_unique_widget_name_badge’ );
$title = $options['title'];
// this is where your plugin code belongs all your mysql and php statements go here…….
// ……………………..
// ………………………
//output to sidebar beginning of widget output
echo $before_widget;
echo $before_title;
echo $title;
echo $after_title;
// pretty up and ready the output from your plugin here
//now print output to webpage
echo “<br> $output <br>”;
//clean up - close db if opened etc here
return $output;
}
//user options Be very sure to at least include the option to change the title
// the other stuff is just to show you how to use the checkbox
function your_very_unique_widget_name_badge_control()
{
$options = get_option ( ‘your_very_unique_widget_name_badge’ );
//set initial values if empty or else fetch current
if ( ! is_array($options) ){
$options = array( ‘title’=>”Default Widget Title”, ‘random’=>”0″ );
}else{
//fetch existing choices from user selected options
$title = $options['title']; // title to show in sidebar
$random = $options ['random'] ? ‘checked=”checked”‘ : “”; // ? yes/no ?
}
//clean up and post
if ( $_POST['your_very_unique_widget_name_badge-submit'] ) {
//title
$options['title'] = strip_tags(stripslashes($_POST['your_very_unique_widget_name_badge-title']));
//yes or no selection box example
$options['yes'] = isset($_POST['your_very_unique_widget_name_badge-yes'] );
//save user selections
update_option(’your_very_unique_widget_name_badge’, $options);
}
// This is the form where we collect the user preferences
// Notice that we don’t need a complete form. This will be embedded into the existing form.
echo ‘<p style=”text-align:right;”><label for=”your_very_unique_widget_name_badge-title”>’ . __(’Title:’) . ‘ <input style=”width: 200px;” id=”your_very_unique_widget_name_badge-title” name=”your_very_unique_widget_name_badge-title” type=”text” value=”‘.$title.’” /></label></p>’;
echo ‘<p style=”text-align:right;”><label for=”your_very_unique_widget_name_badge-yes”>’ . __(’Check for random images’) . ‘ <input style=”width: 20px;” id=”your_very_unique_widget_name_badge-random” name=”your_very_unique_widget_name_badge-random” type=”checkbox” value=”‘.$random.’” /></label></p>’;
//Input our form info if user presses save button
echo ‘<input type=”hidden” id=”your_very_unique_widget_name_badge-submit” name=”your_very_unique_widget_name_badge-submit” value=”1″ />’;
}
//register widget so it is available to user in widget page
register_widget_control(array(’Your Widget Name’, ‘widgets’), ‘your_very_unique_widget_name_badge_control’, 300, 150);
<//register control panel so use can see it and use it
register_sidebar_widget(array(’Your Widget Name’,'widgets’), ‘your_very_unique_widget_name_badge’);
}
// go to main
add_action(’widgets_init’, ‘your_very_unique_widget_name_badge_init’);
?>
0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.
You must log in to post a comment.