WordPress.org

Ready to get started?Download WordPress

Forums

Update instance variables in widget function? (3 posts)

  1. weefselkweekje
    Member
    Posted 4 years ago #

    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;
        }
  2. alieneila
    Member
    Posted 4 years ago #

    Check out this post, might help you out...

    http://blog.firetree.net/2008/11/30/wordpress-multi-widget/

  3. weefselkweekje
    Member
    Posted 4 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic