Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2011
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.98 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: Example Widget
  4.  * Plugin URI: http://example.com/widget
  5.  * Description: A widget that serves as an example for developing more advanced widgets.
  6.  * Version: 0.1
  7.  * Author: Justin Tadlock
  8.  * Author URI: http://justintadlock.com
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13.  */
  14.  
  15. /**
  16.  * Add function to widgets_init that'll load our widget.
  17.  * @since 0.1
  18.  */
  19. add_action( 'widgets_init', 'example_load_widgets' );
  20.  
  21. /**
  22.  * Register our widget.
  23.  * 'Example_Widget' is the widget class used below.
  24.  *
  25.  * @since 0.1
  26.  */
  27. function example_load_widgets() {
  28.     register_widget( 'Example_Widget' );
  29. }
  30.  
  31. /**
  32.  * Example Widget class.
  33.  * This class handles everything that needs to be handled with the widget:
  34.  * the settings, form, display, and update.  Nice!
  35.  *
  36.  * @since 0.1
  37.  */
  38. class Example_Widget extends WP_Widget {
  39.  
  40.     /**
  41.      * Widget setup.
  42.      */
  43.     function Example_Widget() {
  44.         /* Widget settings. */
  45.         $widget_ops = array( 'classname' => 'example', 'description' => __('An example widget that displays a person\'s name and sex.', 'example') );
  46.  
  47.         /* Widget control settings. */
  48.         $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );
  49.  
  50.         /* Create the widget. */
  51.         $this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
  52.     }
  53.  
  54.     /**
  55.      * How to display the widget on the screen.
  56.      */
  57.     function widget( $args, $instance ) {
  58.         extract( $args );
  59.  
  60.         /* Our variables from the widget settings. */
  61.         $title = apply_filters('widget_title', $instance['title'] );
  62.         $name = $instance['name'];
  63.         $sex = $instance['sex'];
  64.         $show_sex = isset( $instance['show_sex'] ) ? $instance['show_sex'] : false;
  65.  
  66.         /* Before widget (defined by themes). */
  67.         echo $before_widget;
  68.  
  69.         /* Display the widget title if one was input (before and after defined by themes). */
  70.         if ( $title )
  71.             echo $before_title . $title . $after_title;
  72.  
  73.         /* Display name from widget settings if one was input. */
  74.         if ( $name )
  75.             printf( '<p>' . __('Hello. My name is %1$s.', 'example') . '</p>', $name );
  76.  
  77.         /* If show sex was selected, display the user's sex. */
  78.         if ( $show_sex )
  79.             printf( '<p>' . __('I am a %1$s.', 'example.') . '</p>', $sex );
  80.  
  81.         /* After widget (defined by themes). */
  82.         echo $after_widget;
  83.     }
  84.  
  85.     /**
  86.      * Update the widget settings.
  87.      */
  88.     function update( $new_instance, $old_instance ) {
  89.         $instance = $old_instance;
  90.  
  91.         /* Strip tags for title and name to remove HTML (important for text inputs). */
  92.         $instance['title'] = strip_tags( $new_instance['title'] );
  93.         $instance['name'] = strip_tags( $new_instance['name'] );
  94.  
  95.         /* No need to strip tags for sex and show_sex. */
  96.         $instance['sex'] = $new_instance['sex'];
  97.         $instance['show_sex'] = $new_instance['show_sex'];
  98.  
  99.         return $instance;
  100.     }
  101.  
  102.     /**
  103.      * Displays the widget settings controls on the widget panel.
  104.      * Make use of the get_field_id() and get_field_name() function
  105.      * when creating your form elements. This handles the confusing stuff.
  106.      */
  107.     function form( $instance ) {
  108.  
  109.         /* Set up some default widget settings. */
  110.         $defaults = array( 'title' => __('Example', 'example'), 'name' => __('John Doe', 'example'), 'sex' => 'male', 'show_sex' => true );
  111.         $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  112.  
  113.         <!-- Widget Title: Text Input -->
  114.         <p>
  115.             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label>
  116.             <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
  117.         </p>
  118.  
  119.         <!-- Your Name: Text Input -->
  120.         <p>
  121.             <label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e('Your Name:', 'example'); ?></label>
  122.             <input id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" value="<?php echo $instance['name']; ?>" style="width:100%;" />
  123.         </p>
  124.  
  125.         <!-- Sex: Select Box -->
  126.         <p>
  127.             <label for="<?php echo $this->get_field_id( 'sex' ); ?>"><?php _e('Sex:', 'example'); ?></label>
  128.             <select id="<?php echo $this->get_field_id( 'sex' ); ?>" name="<?php echo $this->get_field_name( 'sex' ); ?>" class="widefat" style="width:100%;">
  129.                 <option <?php if ( 'male' == $instance['format'] ) echo 'selected="selected"'; ?>>male</option>
  130.                 <option <?php if ( 'female' == $instance['format'] ) echo 'selected="selected"'; ?>>female</option>
  131.             </select>
  132.         </p>
  133.  
  134.         <!-- Show Sex? Checkbox -->
  135.         <p>
  136.             <input class="checkbox" type="checkbox" <?php checked( $instance['show_sex'], true ); ?> id="<?php echo $this->get_field_id( 'show_sex' ); ?>" name="<?php echo $this->get_field_name( 'show_sex' ); ?>" />
  137.             <label for="<?php echo $this->get_field_id( 'show_sex' ); ?>"><?php _e('Display sex publicly?', 'example'); ?></label>
  138.         </p>
  139.  
  140.     <?php
  141.     }
  142. }
  143.  
  144. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement