Advertisement
wpdreams

Wordpress City selector sample widget

Mar 18th, 2014
1,324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.07 KB | None | 0 0
  1. <?php
  2. /**
  3.  * WordPress Sample city selector class
  4.  *
  5.  * Creates a sample widget "My City" with a title and city selector output
  6.  * http://wp-dreams.com/wordpress-widget-select-box/  
  7.  *
  8.  * @author Ernest Marcinko <ernest.marcinko@wp-dreams.com>
  9.  * @version 1.0
  10.  * @link http://wp-dreams.com, http://codecanyon.net/user/anago/portfolio
  11.  * @copyright Copyright (c) 2014, Ernest Marcinko
  12.  */
  13.  
  14. class My_City extends WP_Widget {
  15.  
  16.   public function __construct() {
  17.       $widget_ops = array('classname' => 'My_City', 'description' => 'Displays a city!' );
  18.       $this->WP_Widget('My_City', 'My city', $widget_ops);
  19.   }
  20.  
  21.   function widget($args, $instance) {
  22.     // PART 1: Extracting the arguments + getting the values
  23.     extract($args, EXTR_SKIP);
  24.     $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
  25.     $city = empty($instance['city']) ? '' : $instance['city'];
  26.  
  27.     // Before widget code, if any
  28.     echo (isset($before_widget)?$before_widget:'');
  29.    
  30.     // PART 2: The title and the text output
  31.     if (!empty($title))
  32.       echo $before_title . $title . $after_title;;
  33.     if (!empty($city))
  34.       echo $city;
  35.    
  36.     // After widget code, if any  
  37.     echo (isset($after_widget)?$after_widget:'');
  38.   }
  39.  
  40.   public function form( $instance ) {
  41.    
  42.      // PART 1: Extract the data from the instance variable
  43.      $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
  44.      $title = $instance['title'];
  45.      $city = $instance['city'];  
  46.    
  47.      // PART 2-3: Display the fields
  48.      ?>
  49.      <!-- PART 2: Widget Title field START -->
  50.      <p>
  51.       <label for="<?php echo $this->get_field_id('title'); ?>">Title:
  52.         <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
  53.                name="<?php echo $this->get_field_name('title'); ?>" type="text"
  54.                value="<?php echo attribute_escape($title); ?>" />
  55.       </label>
  56.       </p>
  57.       <!-- Widget Title field END -->
  58.    
  59.      <!-- PART 3: Widget City field START -->
  60.      <p>
  61.       <label for="<?php echo $this->get_field_id('text'); ?>">City:
  62.         <select class='widefat' id="<?php echo $this->get_field_id('city'); ?>"
  63.                 name="<?php echo $this->get_field_name('city'); ?>" type="text">
  64.           <option value='New York'<?php echo ($city=='New York')?'selected':''; ?>>
  65.             New York
  66.           </option>
  67.           <option value='Los Angeles'<?php echo ($city=='Los Angeles')?'selected':''; ?>>
  68.             Los Angeles
  69.           </option>
  70.           <option value='Boston'<?php echo ($city=='Boston')?'selected':''; ?>>
  71.             Boston
  72.           </option>
  73.         </select>                
  74.       </label>
  75.      </p>
  76.      <!-- Widget City field END -->
  77.      <?php
  78.    
  79.   }
  80.  
  81.   function update($new_instance, $old_instance) {
  82.     $instance = $old_instance;
  83.     $instance['title'] = $new_instance['title'];
  84.     $instance['city'] = $new_instance['city'];
  85.     return $instance;
  86.   }
  87.  
  88. }
  89.  
  90. add_action( 'widgets_init', create_function('', 'return register_widget("My_City");') );
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement