Custom address widget

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’]);
$country = esc_attr($instance[‘country’]);
$phone = esc_attr($instance[‘phone’]);
$email = esc_attr($instance[’email’]);
$city = esc_attr($instance[‘city’]);

} else {
$title = ”;
$country = ”;
$phone = ”;
$email = ”;
$city = ”;
}
?>

<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(‘country’); ?>”><?php _e(‘country:’, ‘wp_widget_plugin’); ?></label>
<input id=”<?php echo $this->get_field_id(‘country’); ?>” name=”<?php echo $this->get_field_name(‘country’); ?>” type=”text” value=”<?php echo $country; ?>” />
</p>

<p>
<label for=”<?php echo $this->get_field_id(‘city’); ?>”><?php _e(‘city:’, ‘wp_widget_plugin’); ?></label>
<input id=”<?php echo $this->get_field_id(‘city’); ?>” name=”<?php echo $this->get_field_name(‘city’); ?>” type=”text” value=”<?php echo $city; ?>” />
</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>

<?php
}

public function update($new_instance, $old_instance) {
$instance = $old_instance;
// Fields
$instance[‘title’] = strip_tags($new_instance[‘title’]);
$instance[‘country’] = strip_tags($new_instance[‘country’]);
$instance[‘city’] = strip_tags($new_instance[‘city’]);
$instance[‘phone’] = strip_tags($new_instance[‘phone’]);
$instance[’email’] = strip_tags($new_instance[’email’]);

return $instance;
}

// display widget
public function widget($args, $instance) {
extract( $args );
// these are the widget options
$title = apply_filters(‘widget_title’, $instance[‘title’]);
$country = $instance[‘country’];
$city = $instance[‘city’];
$phone = $instance[‘phone’];
$email = $instance[’email’];

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( $country ) {
echo ‘<li><span>Country:</span>’.$country.'</li>’;
}
if( $city ) {
echo ‘<li><span>City:</span>’.$city.'</li>’;
}
if( $phone ) {
echo ‘<li><span>Phone:</span>’.$phone.'</li>’;
}
if( $email ) {
echo ‘<li><span>Email:</span><a href=”#”>   ‘.$email.'</a></li>’;
}

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

}
register_widget( ‘My_Widget’ );

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