Advertisement
Guest User

Add Feature Widget

a guest
Dec 21st, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.92 KB | None | 0 0
  1. add_action( 'widgets_init', array ( 'T5_Array_Options_Widget', 'register' ) );
  2.  
  3. class T5_Array_Options_Widget extends WP_Widget
  4. {
  5.     /**
  6.      * Constructor.
  7.      */
  8.     public function __construct()
  9.     {
  10.         parent::__construct( strtolower( __CLASS__ ), 'Array Demo' );
  11.        
  12.         wp_enqueue_script( 'pricebox', get_template_directory_uri() . '/js/pricetable.js','','',true);
  13.     }
  14.  
  15.     /**
  16.      * Echo the settings update form
  17.      *
  18.      * @param array $instance Current settings
  19.      */
  20.     public function form( $instance )
  21.     {
  22.         $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'fields' => '' ) );
  23.         $title = isset ( $instance['title'] ) ? $instance['title'] : '';
  24.         $title = esc_attr( $title );
  25.  
  26.         printf(
  27.             '<p><label for="%1$s">%2$s</label><br />
  28.             <input type="text" name="%3$s" id="%1$s" value="%4$s" class="widefat"></p>',
  29.             $this->get_field_id( 'title' ),
  30.             'Title',
  31.             $this->get_field_name( 'title' ),
  32.             $title
  33.         );
  34.  
  35.         $fields = isset ( $instance['fields'] ) ? $instance['fields'] : array();
  36.         $field_num = count( $fields );
  37.         $fields[ $field_num ] = '';
  38.         $fields_html = array();
  39.         $fields_counter = 0;
  40.         foreach ( $fields as $name => $value )
  41.         {
  42.             $fields_html[] = sprintf(
  43.                 '<input type="text" name="%1$s[%2$s]" value="%3$s" id="%4$s" class="widefat feature%2$s">',
  44.                 $this->get_field_name( 'fields' ),
  45.                 $fields_counter,
  46.                 esc_attr( $value ),
  47.                 $this->get_field_id( 'fields' )
  48.             );
  49.             $fields_counter += 1;
  50.             if ($fields_counter == $field_num) break;
  51.         }        
  52.         print 'Fields<br />' . join( '<br />', $fields_html );
  53.         ?>
  54.         <script type="text/javascript">
  55.             var fieldname = <?php echo json_encode($this->get_field_name( 'fields' )) ?>;
  56.             var fieldnum = <?php echo json_encode($fields_counter - 1) ?>;
  57.            
  58.             jQuery(function($) {
  59.                 var count = fieldnum;
  60.                 $('.<?php echo $this->get_field_id('addfeature') ?>').click(function() {
  61.                     $( ".feature"+count).after("<input type='text' name='"+fieldname+"["+(count+1) +"]' value='' class='widefat feature"+ (count+1) +"'>" );
  62.                     count++;
  63.                    
  64.                 });
  65.             });
  66.         </script>
  67.         <?php
  68.         echo '<input class="button '.$this->get_field_id('addfeature').'" type="button" value="' . __( 'Add Feature', 'myvps' ) . '" id="addfeature" />';
  69.     }
  70.  
  71.     /**
  72.      * Renders the output.
  73.      *
  74.      * @see WP_Widget::widget()
  75.      */
  76.     public function widget( $args, $instance )
  77.     {
  78.         print $args['before_widget']
  79.         . $args['before_title']
  80.         . apply_filters( 'widget_title', $instance['title'] )
  81.         . $args['after_title']
  82.         . join( '<br />', $instance['fields'] )
  83.         . $args['after_widget'];
  84.     }
  85.  
  86.     /**
  87.      * Prepares the content. Not.
  88.      *
  89.      * @param  array $new_instance New content
  90.      * @param  array $old_instance Old content
  91.      * @return array New content
  92.      */
  93.     public function update( $new_instance, $old_instance )
  94.     {
  95.         $instance          = $old_instance;
  96.         $instance['title'] = esc_html( $new_instance['title'] );
  97.         $instance['fields'] = array();
  98.  
  99.         if ( isset ( $new_instance['fields'] ) )
  100.         {
  101.             foreach ( $new_instance['fields'] as $value )
  102.             {
  103.                 if ( '' !== trim( $value ) )
  104.                     $instance['fields'][] = $value;
  105.             }
  106.         }
  107.  
  108.         return $instance;
  109.  
  110.     }
  111.  
  112.     /**
  113.      * Tell WP we want to use this widget.
  114.      *
  115.      * @wp-hook widgets_init
  116.      * @return void
  117.      */
  118.     public static function register()
  119.     {
  120.         register_widget( __CLASS__ );
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement