• Hey everybody! (first post)

    I have coded a widget that users should be able to put into ONE sidebar multiple times. A single instance works perfect, but if i add the second instance, wierdly, only the first field is saved correctly.

    i will post the whole code, as i dont really have a clue why thats happening, as multi instance should be handled automatically since 2.8 as far as i understand it.

    here it is:

    <?php add_action ('widgets_init', 'EVWidgetInit');
    
    //Declaring Widgets
    function EVWidgetInit() {
    	register_widget('EVOffice_widget');
    
    	}
    class EVOffice_widget extends WP_Widget {
    
    	function EVOffice_widget() {
    		$init_args = array(
    		'classname' => 'Office_Widget_Container',
    		'description' => __('Flyouts for Office Display in Sidebars')
    		);
    		$this->WP_Widget ('EVOffice_widget', __('E&V Offices Widget'), $init_args);
    		}
    
    	function form($instance) {
    
    		//Setting defaults
    		$defaults = array(
    		'title' => 'Our Offices',
    		'Headline' => 'Your Engel & V?lkers Office',
    		'Address' => '',
    		'PostCode' => '',
    		'Location' => '',
    		'Country' => '',
    		'Phone' => '',
    		'Phone2Text' => 'Rentals',
    		'Phone2' => '',
    		'Fax' => '',
    		'Email' => '',
    		'Tabs' => 'false',
    		'OfficeIndex' => 1
    		);
    
    		$instance = wp_parse_args( (array) $instance, $defaults);
    		//Make it more readable
    		foreach($instance as $key => $value) {
    
    			$$key = $value;
    
    			}
    
    		//Stuff for image
    
    		//Building the admin form
    		?>
    
    		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Office Title (for Tab Header)'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value = "<?php echo esc_attr($title); ?>" />
            </label></p>
    
    		<p><label for="<?php echo $this->get_field_id( 'Headline'); ?>"><?php _e( 'Office Headline (e.g. "Office Ibiza")'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id( 'Headline' ); ?>" name="<?php echo $this->get_field_name( 'Headline' ); ?>" type="text" value = "<?php echo esc_attr($Headline); ?>" />
            </label></p>
    		<p><label for="<?php echo $this->get_field_id( 'Address'); ?>"><?php _e( 'Street Adress'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id( 'Address' ); ?>" name="<?php echo $this->get_field_name( 'Address' ); ?>" type="text" value = "<?php echo esc_attr( $Address ); ?>" />
            </label></p>
            <p><label for="<?php echo $this->get_field_id( 'PostCode'); ?>"><?php _e( 'Postcode eg. 07800'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id( 'PostCode' ); ?>" name="<?php echo $this->get_field_name( 'PostCode' ); ?>" type="text" value = "<?php echo esc_attr( $PostCode ); ?>" /></label></p>
            <p><label for="<?php echo $this->get_field_id( 'Location'); ?>"><?php _e( 'City, e.g. Sta. Eulalia'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id( 'Location' ); ?>" name="<?php echo $this->get_field_name( 'Location' ); ?>" type="text" value = "<?php echo esc_attr( $Location ); ?>" />
            </label></p>
            <p><label for="<?php echo $this->get_field_id( 'Country'); ?>"><?php _e( 'Country e.g. Spain'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id( 'Country' ); ?>" name="<?php echo $this->get_field_name( 'Country' ); ?>" type="text" value = "<?php echo esc_attr( $Country ); ?>" />
            </label></p>
            <p><label for="<?php echo $this->get_field_id( 'Phone'); ?>"><?php _e( 'Phone Number (international Notation)'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id( 'Phone' ); ?>" name="<?php echo $this->get_field_name( 'Phone' ); ?>" type="text" value = "<?php echo esc_attr( $Phone ); ?>" />
            </label></p>
            <p><label for="<?php echo $this->get_field_id( 'Phone2Text'); ?>"><?php _e( 'Text for 2nd Phone Number (optional) e.g. "Rentals"'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id( 'Phone2Text' ); ?>" name="<?php echo $this->get_field_name( 'Phone2Text' ); ?>" type="text" value = "<?php echo esc_attr( $Phone2Text ); ?>" />
            </label></p>
            <p><label for="<?php echo $this->get_field_id( 'Phone2'); ?>"><?php _e( 'Secondary Phone (optional)'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id( 'Phone2' ); ?>" name="<?php echo $this->get_field_name( 'Phone2' ); ?>" type="text" value = "<?php echo esc_attr( $Phone2 ); ?>" />
            </label></p>
             <p><label for="<?php echo $this->get_field_id( 'Fax'); ?>"><?php _e( 'Fax (optional)'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id( 'Fax' ); ?>" name="<?php echo $this->get_field_name( 'Fax' ); ?>" type="text" value = "<?php echo esc_attr( $Fax ); ?>" />
            </label></p>
            <p><label for="<?php echo $this->get_field_id( 'Email'); ?>"><?php _e( 'Office Email (for contact form)'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id( 'Email' ); ?>" name="<?php echo $this->get_field_name( 'Email' ); ?>" type="text" value = "<?php echo esc_attr( $Email ); ?>" />
            </label></p>
            <p><label for="<?php echo $this->get_field_id('Tabs'); ?>"><?php _e( 'For Tabbed Display (mult. Offices), set to true'); ?>
            <select name="<?php echo $this->get_field_name( 'Tabs' ); ?>" id="<?php echo $this->get_field_id( 'Tabs' ); ?>">
              <option value="false" <?php if(esc_attr( $Tabs ) == 'false') echo 'selected="selected"'; ?>>False</option>
              <option value="true" <?php if(esc_attr( $Tabs ) == 'true') echo 'selected="selected"'; ?>>True</option>
            </select>
            </label></p>
            <p><label for="<?php echo $this->get_field_id( 'OfficeIndex'); ?>"><?php _e( 'For multiple Office, set Order here'); ?>
            <select name="<?php echo $this->get_field_name( 'OfficeIndex' ); ?>" id="<?php echo $this->get_field_id( 'OfficeIndex' ); ?>">
              <option value="1" <?php selected(esc_attr($OfficeIndex), 1) ?>>1</option>
              <option value="2" <?php selected(esc_attr($OfficeIndex), 2) ?>>2</option>
              <option value="3" <?php selected(esc_attr($OfficeIndex), 3) ?>>3</option>
              <option value="4" <?php selected(esc_attr($OfficeIndex), 4) ?>>4</option>
              <option value="5" <?php selected(esc_attr($OfficeIndex), 5) ?>>5</option>
            </select>
            </label></p>
    
    <?php
    //ENd of Form initialization
    		}
    
    public function update($new_instance, $old_instance)
    	{
    		$instance = $old_instance;
    
    foreach($instance as $key => $value) {
    
    			$instance[$key] = strip_tags( $new_instance[$key]);
    
    			}
    
    	return $instance;
    	 }
    
    function widget($args, $instance) {
    		extract($args); 
    
    		echo $before_widget;
    ?>
    			<div class="OfficeWidget row">
    
          <div class="col-xs-12 hidden-sm col-md-4">
    
    </div>
    <div class="col-xs-12 col-sm-12 col-md-8">
            <div class="info">
              <h2  itemprop="name"><?php  _e($instance['Headline']); ?></h2>
              <address  itemprop="address" itemscope="" itemtype="https://schema.org/PostalAddress">
              <span  itemprop="streetAddress"><?php _e($instance['Address']); ?></span>
    <span  itemprop="postalCode"><?php _e($instance['PostCode']); ?></span> <span  itemprop="addressLocality"><?php _e($instance['Location']); ?></span>
              <span  itemprop="addressCountry">| <?php _e($instance['Country']); ?></span>
              </address>
              <dl>
                <dt>Phone</dt>
                <dd itemprop="telephone"><?php _e($instance['Phone']); ?></dd>
                <?php if (isset($instance['Phone2Text']) && $instance['Phone2Text'] != "") { ?>
                <dt><?php _e($instance['Phone2Text']); ?></dt>
                <dd itemprop="telephone"><?php _e($instance['Phone2']); ?></dd>
                <?php } ?>
                <?php if (isset($instance['Fax']) && $instance['Fax'] != "") { ?>
                <dt>Fax</dt>
                <dd itemprop="faxNumber"><?php _e($instance['Fax']); ?></dd>
                <?php } ?>
              </dl>
            </div>
            <div class="contact-link"> <a href="">Contact</a> </div>
          </div>
          <!-- End Office Ibiza Tab -->
          </div>
    
    <?PHP
    //End if Tabs are True
    	 echo $after_widget;
    	?>
    
    <?php //End of Widget Display
    	} 
    
    	//End of EV Office WIdget Class
    }
    ?>

    [Moderator Note: Please post code or markup between backticks (not single quotes) or use the code button. Or better still – use a pastebin. Your posted code may now have been permanently damaged by the forum’s parser.]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter AlexIbiza

    (@alexibiza)

    Sorry, first post, as i said.
    I narrowed down the problem, seems to be an issues with using

    ‘foreach($instance as $key => $value) {
    $instance[$key] = strip_tags( $new_instance[$key]);
    }’

    in the update function to generate variable mapping.

    Is this not allowed ?

    Thanks!

    Thread Starter AlexIbiza

    (@alexibiza)

    OK, for anybody stumbling upon the same issue, actually writing it out makes it work – so if you have somehting like:

    public function update($new_instance, $old_instance)
    	{
    		$instance = $old_instance;
    
    'foreach($instance as $key => $value) {
    $instance[$key] = strip_tags( $new_instance[$key]);
    }

    using the “written out form” like this:

    public function update($new_instance, $old_instance)
    	{
    		$instance = $old_instance;
    
    $instance['title'] = strip_tags( $new_instance['title']);
    	$instance['Headline'] = strip_tags( $new_instance['Headline']);
    	...
    
    	 }'

    works – although i do not have a clue why this works like that, as the foreach method should actually do the same – or am i having a thinking problem there ?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Multiple instances of custom widget’ is closed to new replies.