Wordpress Shortcodes, Actions, Events Manager, TweetBlender
I'm working on a Wordpress project which uses David Benini's "Events Manager" plugin. The events manager plugin does not use Wordpress' posts for Events. Instead it creates an page with the title "Events", whose URL would be http://yourdomain.com/events. The content rendered on this page varies. The plugin uses the query string to fetch a single event or all the events for a day, etc. The results from it's query for events are added as filter to the Wordpress function the_content().
Goal
Allow the client to easily specify a Twitter #hash tag when creating a new event. Get those tweets and render them on the events page, beneath the main event details and rsvp form. I found the Wordpress TweetBlender plugin to fetch tweets by #hashtag. TweetBlender has a widget function that lets you pass params for the tweets in your Wordpress theme code.
Problem 1 - How to store and pass the twitter topic
Typically if I wanted to pass along info such as a twitter hash tag, I would do so using the WP Custom Fields. But "Events Manager" does not use posts as events so this was not an option. I could have hacked the Events Manager plugin and added another field in the database tables, e.g., event_meta_values. But I always try to find a solution other than hacking plugins. I realized I could use the WP Shortcodes API to pass along the info about the twitter hash for each event. E.g.,
[twitter_hash topic=primary_elections]
I was easily able to extract the info from the shortcode using Wordpress' shortcode API. Next, I wanted to feed this topic into the tweet blender widget function, which outputs a list of tweets based on the twitter hashtag or username you pass it.
Problem 2 - When to output the TweetBlender data
Normally, the value returned by a shortcode handler function is added as a filter to the Wordpress function the_content(). But for my purposes, I wanted the tweets to be at the bottom of the page beneath the main event details and rsvp form. So the question was how to hook into the wordpress Actions API in order to render the tweet blender output in an appropriate place on the page. Reading the WP Actions API docs, I found an action called "loop_end". As the name suggests, the docs stated that any function called at that time would run immediately after the_loop has ended.
Implementation
As is often the case when I code without planning, I went about coding this all the wrong way. Initially I was catching the twitter topic in the shortcode handler function. Then I passed the twitter topic to the TweetBlender Widget function. That function echoes the tweets immediately. And the return value from a shortcode handler function is added to the_content(). So the tweets were showing up in the center of the Events page. My elaborate attempt to fix this was having the shortcode handler function return FALSE. Then I was capturing the output of TweetBlender using php functions like ob_start(), get_buffer_contents(), and ob_end_clean(). Here is what I ultimately ended up with. Would love some input on if there is an easier way.
function twitter_hash($atts, $content = null){ global $topic; extract(shortcode_atts(array('topic' => '#voting',), $atts)); return FALSE; } if (function_exists('twitter_hash')): add_shortcode('twitter_hash', 'twitter_hash'); endif;
function get_tw_data(){ global $topic; if ( !empty($topic) || !empty($GLOBALS['topic']) ): if ( function_exists('tweet_blender_widget') ): $args = array( 'unique_div_id' => 'tweetblender-t1', 'sources' => $topic, 'refresh_rate' => 60, 'tweets_num' => 5, 'view_more_url' => 'http://twitter.com/tweetblender' ); tweet_blender_widget($args); unset($GLOBALS['topic']); unset($topic); endif; else: return FALSE; endif; } if (function_exists('get_tw_data')): add_action('loop_end','get_tw_data'); endif;














