WordPress.org

Support

Support » Plugins and Hacks » Update instance variables in widget function?

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)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Update instance variables in widget function?’ is closed to new replies.
Skip to toolbar