To add a custom widget just create a plugin by following below steps.
1. Create a folder in plugins folder.
2. Add a php file with folder name(Suppose if folder name is abc the file in folder should abc.php).
3. Add below code in that file.
<?php
/*
Plugin Name: Widget Plugin Name
Plugin URI: Author Link
Description: A simple plugin that adds a calendar of events at rights side
Version: 1.0
Author: Author Name
Author URI: Author Link
License: GPL2
*/
class wp_my_plugin extends WP_Widget {
// constructor
function wp_my_plugin() {
parent::WP_Widget(false, $name = __('My Widget', 'wp_widget_plugin') );
}
// widget form creation
function form($instance) {
// Check values
if( $instance) {
$title = esc_attr($instance['title']);
} else {
$title = '';
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title', 'wp_widget_plugin'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<?php
}
// update widget
function update($new_instance, $old_instance) {
$instance = $old_instance;
// Fields
$instance['title'] = strip_tags($new_instance['title']);
$instance['text'] = strip_tags($new_instance['text']);
$instance['textarea'] = strip_tags($new_instance['textarea']);
return $instance;
}
// display widget
function widget($args, $instance) {
extract( $args );
// these are the widget options
$title = apply_filters('widget_title', $instance['title']);
$text = $instance['text'];
$textarea = $instance['textarea'];
echo $before_widget;
// Display the widget
echo '<div class="widget-text wp_widget_plugin_box">';
// Check if title is set
if ( $title ) {
echo $before_title . $title . $after_title;
}
//add functionality you want add here..
echo '</div>';
echo $after_widget;
}
}
// register widget
add_action('widgets_init', create_function('', 'return register_widget("wp_my_plugin");'));
?>
No comments:
Post a Comment