Importing Tweets with certain hashtags into WordPress

I wrote a shortcode function based on “Twitter Hash Tag Widget” plugin

just copy this function to your themes functions.php file

function tweets_by_hashtag_9867($atts, $content = null){
            extract(shortcode_atts(array(
                "hashtag" => 'default_tag',
                "number" => 5,
                ), $atts));
        $api_url="http://search.twitter.com/search.json";
        $raw_response = wp_remote_get("$api_url?q=%23$hashtag&rpp=$number");

        if ( is_wp_error($raw_response) ) {
            $output = "<p>Failed to update from Twitter!</p>\n";
            $output .= "<!--{$raw_response->errors['http_request_failed'][0]}-->\n";
            $output .= get_option('twitter_hash_tag_cache');
        } else {
            if ( function_exists('json_decode') ) {
                $response = get_object_vars(json_decode($raw_response['body']));
                for ( $i=0; $i < count($response['results']); $i++ ) {
                    $response['results'][$i] = get_object_vars($response['results'][$i]);
                }
            } else {
                include(ABSPATH . WPINC . '/js/tinymce/plugins/spellchecker/classes/utils/JSON.php');
                $json = new Moxiecode_JSON();
                $response = @$json->decode($raw_response['body']);
            }

            $output = "<div class="twitter-hash-tag">\n";
            foreach ( $response['results'] as $result ) {
                $text = $result['text'];
                $user = $result['from_user'];
                $image = $result['profile_image_url'];
                $user_url = "http://twitter.com/$user";
                $source_url = "$user_url/status/{$result['id']}";

                $text = preg_replace('|(https?://[^\ ]+)|', '<a href="https://wordpress.stackexchange.com/questions/9447/">$1</a>', $text);
                $text = preg_replace('|@(\w+)|', '<a href="http://twitter.com/$1">@$1</a>', $text);
                $text = preg_replace('|#(\w+)|', '<a href="http://search.twitter.com/search?q=%23$1">#$1</a>', $text);

                $output .= "<div>";

                if ( $images )
                    $output .= "<a href="$user_url"><img src="$image" alt="$user" /></a>";
                $output .= "<a href="$user_url">$user</a>: $text <a href="$source_url">&raquo;</a></div>\n";
            }
            $output .= "<div class="view-all"><a href="http://search.twitter.com/search?q=%23$hashtag">" . __('View All') . "</a></div>\n";
            $output .= "</div>\n";
        }

        return $output;
}

then turn it into a short code by adding the next line to your themes functions.php file:

add_shortcode("hashtag_tweets", "tweets_by_hashtag_9867");

Then all you have left to do is create a new,page or post and enter a shortcode for example:

[hashtag_tweets hashtag="YOUR_TAG" number="NUMBER_OF_TWEETS_TO_GET"]

change YOUR_TAG to your hashtag and change NUMBER_OF_TWEETS_TO_GET to the nuber of tweets you want to get.

Hope this helps.


Leave a Comment

tech