Title: Update instance variables in widget function?
Last modified: August 19, 2016

---

# Update instance variables in widget function?

 *  [weefselkweekje](https://wordpress.org/support/users/weefselkweekje/)
 * (@weefselkweekje)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/update-instance-variables-in-widget-function/)
 * I’m trying to create a simple widget that fetches tweets from the Twitter API
   and displays them. To keep things speedy I need to cache the results.
 * Because I want this widget to allow multiple instances, the $instance variables
   seem like the right place to store the cached output. But since the output that
   needs to be cached is fetched in the ‘widget’ function of the API, I’d need to
   update $instance from there.
 * Is this possible? It seems that only the update function (called when the options
   panel is saved) does this, and $instance is read-only in the widget function?
 * Here’s a simplified version of what I currently have:
 *     ```
       /** @see WP_Widget::widget */
           function widget($args, $instance) {
               extract( $args );
       		$lastupdate = $instance["lastupdate"];
       		if ( $lastupdate < ( mktime() - (10*60) ) ) {
       			// stuff to get the tweets, store in $output var
       			$instance["tweetcache"] = $output;
       			$instance["lastupdate"] = mktime();
       		} else {
       			$output = $instance["tweetcache"];
       		}
       		echo $output;
           }
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [alieneila](https://wordpress.org/support/users/alieneila/)
 * (@alieneila)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/update-instance-variables-in-widget-function/#post-1473669)
 * Check out this post, might help you out…
 * [http://blog.firetree.net/2008/11/30/wordpress-multi-widget/](http://blog.firetree.net/2008/11/30/wordpress-multi-widget/)
 *  Thread Starter [weefselkweekje](https://wordpress.org/support/users/weefselkweekje/)
 * (@weefselkweekje)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/update-instance-variables-in-widget-function/#post-1473692)
 * Thanks, but since this is client work, I’d like to stick with the regular API
   for now, as per their request. Seems to me like simply being able to update an
   instance’s vars should be trivial?

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Update instance variables in widget function?’ is closed to new replies.

## Tags

 * [api](https://wordpress.org/support/topic-tag/api/)
 * [cache](https://wordpress.org/support/topic-tag/cache/)
 * [instance](https://wordpress.org/support/topic-tag/instance/)
 * [variables](https://wordpress.org/support/topic-tag/variables/)
 * [widget](https://wordpress.org/support/topic-tag/widget/)

 * 2 replies
 * 2 participants
 * Last reply from: [weefselkweekje](https://wordpress.org/support/users/weefselkweekje/)
 * Last activity: [16 years, 1 month ago](https://wordpress.org/support/topic/update-instance-variables-in-widget-function/#post-1473692)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
