javascript - Including twitter/flickr api calls on every page load (WordPress) -
i want include latest flickr photos , twitter status updates in wordpress sidebar or footer using jsonp requests. problem of course each page reload perform new ajax call, , believe twitter api has usage limits.
i considering cross-browser data persistance options had javascript , best think of cookies.
would storing ajax results in cookie (setting expiry day, dont update twitter/flickr often). best solution javascript-based twitter , flickr api call?
thanks
the best solution javascript-based twitter , flickr api calls when using wordpress use wordpress transient api. transient api persistent cache method built in wordpress meant cache items change frequently. can set cache expires time , wordpress check database first transient if returns false use json call return item.
here example using transient , shortcodes store users recent tweet. code below aaron jorbin twitter transients plugin
function twitter_status($atts){ extract(shortcode_atts(array( 'screenname' => '', 'count' => 1 ), $atts)); $transient = "$screenname"."_$count"."_twitter_status"; $statuses = get_transient($transient); if ($statuses == true ) { return $statuses; } elseif ($screenname != false) { $site = "http://twitter.com/statuses/user_timeline.json?screen_name=$screenname&count=$count"; $ch = curl_init(); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_url, $site); $result = curl_exec($ch); $tweets = json_decode($result); ob_start(); foreach ( (array) $tweets $tweet){ $tweetcontent = $tweet->text; $newcontent = preg_replace('%@([^\s]*)%', "<a href="http://twitter.com/\\1">@\\1</a>", $tweetcontent); echo "<div class="twitter_shortcode"><p> <img class="twitter_shortcode_image" src="".esc_url($tweet->user->profile_image_url).""><span class="twitter_shotcode_username"><a href="http://twitter.com/".$tweet->user->screen_name."">".$tweet->user->screen_name."</a> — </span>$newcontent</p> </div>"; } $tweet_display = ob_get_clean(); set_transient($transient, $tweet_display, 120); return $tweet_display; } else { return false; } } add_shortcode('twitter_status', 'twitter_status');
Comments
Post a Comment