Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'widgets_init', array ( 'T5_Array_Options_Widget', 'register' ) );
- class T5_Array_Options_Widget extends WP_Widget
- {
- /**
- * Constructor.
- */
- public function __construct()
- {
- parent::__construct( strtolower( __CLASS__ ), 'Array Demo' );
- wp_enqueue_script( 'pricebox', get_template_directory_uri() . '/js/pricetable.js','','',true);
- }
- /**
- * Echo the settings update form
- *
- * @param array $instance Current settings
- */
- public function form( $instance )
- {
- $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'fields' => '' ) );
- $title = isset ( $instance['title'] ) ? $instance['title'] : '';
- $title = esc_attr( $title );
- printf(
- '<p><label for="%1$s">%2$s</label><br />
- <input type="text" name="%3$s" id="%1$s" value="%4$s" class="widefat"></p>',
- $this->get_field_id( 'title' ),
- 'Title',
- $this->get_field_name( 'title' ),
- $title
- );
- $fields = isset ( $instance['fields'] ) ? $instance['fields'] : array();
- $field_num = count( $fields );
- $fields[ $field_num ] = '';
- $fields_html = array();
- $fields_counter = 0;
- foreach ( $fields as $name => $value )
- {
- $fields_html[] = sprintf(
- '<input type="text" name="%1$s[%2$s]" value="%3$s" id="%4$s" class="widefat feature%2$s">',
- $this->get_field_name( 'fields' ),
- $fields_counter,
- esc_attr( $value ),
- $this->get_field_id( 'fields' )
- );
- $fields_counter += 1;
- if ($fields_counter == $field_num) break;
- }
- print 'Fields<br />' . join( '<br />', $fields_html );
- ?>
- <script type="text/javascript">
- var fieldname = <?php echo json_encode($this->get_field_name( 'fields' )) ?>;
- var fieldnum = <?php echo json_encode($fields_counter - 1) ?>;
- jQuery(function($) {
- var count = fieldnum;
- $('.addfeature').click(function() {
- $( ".feature"+count).after("<input type='text' name='"+fieldname+"["+(count+1) +"]' value='' class='widefat feature"+ (count+1) +"'>" );
- count++;
- });
- });
- </script>
- <?php
- echo '<input class="button addfeature" type="button" value="' . __( 'Add Feature', 'myvps' ) . '" id="addfeature" />';
- }
- /**
- * Renders the output.
- *
- * @see WP_Widget::widget()
- */
- public function widget( $args, $instance )
- {
- print $args['before_widget']
- . $args['before_title']
- . apply_filters( 'widget_title', $instance['title'] )
- . $args['after_title']
- . join( '<br />', $instance['fields'] )
- . $args['after_widget'];
- }
- /**
- * Prepares the content. Not.
- *
- * @param array $new_instance New content
- * @param array $old_instance Old content
- * @return array New content
- */
- public function update( $new_instance, $old_instance )
- {
- $instance = $old_instance;
- $instance['title'] = esc_html( $new_instance['title'] );
- $instance['fields'] = array();
- if ( isset ( $new_instance['fields'] ) )
- {
- foreach ( $new_instance['fields'] as $value )
- {
- if ( '' !== trim( $value ) )
- $instance['fields'][] = $value;
- }
- }
- return $instance;
- }
- /**
- * Tell WP we want to use this widget.
- *
- * @wp-hook widgets_init
- * @return void
- */
- public static function register()
- {
- register_widget( __CLASS__ );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement