Create Custom Widget In WordPress

class My_Widget extends WP_Widget {

public function __construct() {
parent::__construct(‘My_Widget’, __(‘Custom Widget’), array( ‘description’ => __( ‘Sample widget based on Custom Widget’), ) );
}

public function form($instance) {

// Check values
if( $instance) {
$title = esc_attr($instance[‘title’]);
$address = esc_attr($instance[‘address’]);
$phone = esc_attr($instance[‘phone’]);
$email = esc_attr($instance[’email’]);
$web = esc_attr($instance[‘web’]);

} else {
$title = ”;
$address = ”;
$phone = ”;
$email = ”;
$web = ”;
}
?>

<p>
<label for=”<?php echo $this->get_field_id(‘title’); ?>”><?php _e(‘Widget Title:’, ‘wp_widget_plugin’); ?></label>
<input id=”<?php echo $this->get_field_id(‘title’); ?>” name=”<?php echo $this->get_field_name(‘title’); ?>” type=”text” value=”<?php echo $title; ?>” />
</p>

<p>
<label for=”<?php echo $this->get_field_id(‘address’); ?>”><?php _e(‘Address:’, ‘wp_widget_plugin’); ?></label>
<input id=”<?php echo $this->get_field_id(‘address’); ?>” name=”<?php echo $this->get_field_name(‘address’); ?>” type=”text” value=”<?php echo $address; ?>” />
</p>

<p>
<label for=”<?php echo $this->get_field_id(‘phone’); ?>”><?php _e(‘Phone:’, ‘wp_widget_plugin’); ?></label>
<input id=”<?php echo $this->get_field_id(‘phone’); ?>” name=”<?php echo $this->get_field_name(‘phone’); ?>” type=”text” value=”<?php echo $phone; ?>” />
</p>

<p>
<label for=”<?php echo $this->get_field_id(‘email’); ?>”><?php _e(‘Email:’, ‘wp_widget_plugin’); ?></label>
<input id=”<?php echo $this->get_field_id(‘email’); ?>” name=”<?php echo $this->get_field_name(‘email’); ?>” type=”text” value=”<?php echo $email; ?>” />
</p>

<p>
<label for=”<?php echo $this->get_field_id(‘web’); ?>”><?php _e(‘Phone:’, ‘wp_widget_plugin’); ?></label>
<input id=”<?php echo $this->get_field_id(‘web’); ?>” name=”<?php echo $this->get_field_name(‘web’); ?>” type=”text” value=”<?php echo $web; ?>” />
</p>
<?php
}

public function update($new_instance, $old_instance) {
$instance = $old_instance;
// Fields
$instance[‘title’] = strip_tags($new_instance[‘title’]);
$instance[‘address’] = strip_tags($new_instance[‘address’]);
$instance[‘phone’] = strip_tags($new_instance[‘phone’]);
$instance[’email’] = strip_tags($new_instance[’email’]);
$instance[‘web’] = strip_tags($new_instance[‘web’]);
return $instance;
}

// display widget
public function widget($args, $instance) {
extract( $args );
// these are the widget options
$title = apply_filters(‘widget_title’, $instance[‘title’]);
$address = $instance[‘address’];
$phone = $instance[‘phone’];
$email = $instance[’email’];
$web = $instance[‘web’];
echo $before_widget;
// Display the widget
echo ‘<div>’;

// Check if title is set
if ( $title ) {
echo $before_title . $title . $after_title;
}

// Check if text is set
if( $address ) {
echo ‘<p>Address:’.$address.’</p>’;
}
if( $phone ) {
echo ‘<p>Phone:’.$phone.’</p>’;
}
if( $email ) {
echo ‘<p>Email:’.$email.’</p>’;
}
if( $web ) {
echo ‘<p>Web:’.$web.’</p>’;
}

echo ‘</div>’;
echo $after_widget;
}

}
register_widget( ‘My_Widget’ );

//using Form Textarea, Checkbox and Select Box

http://www.wpexplorer.com/how-to-create-a-widget-plugin-for-wordpress-part-2/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s