Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm looking for a way to import tweets into Wordpress as posts. In fact, I want to display tweets about a certain topic on a page in Wordpress. So page A would contain tweets about hashtag x, and page B would contain tweets hashtag y.

There is at least one plugin that imports tweets (Tweet-Import), but it can only import tweets by a certain user, not by hashtag.

Is there any way that this can be done?

Cheers!

share|improve this question
    
How do you mean "Import tweets". Do you just want a page that generates a list of recent tweets with a certain hashtag, or do you want to put the tweets into the database? –  John P Bloch Feb 15 '11 at 21:53
    
It's preferable to import them into the database, but the first option is good too. –  user3201 Feb 15 '11 at 21:59
add comment

1 Answer

up vote 11 down vote accepted

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="$1">$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.

share|improve this answer
1  
Absolutely fantastic. Thank you so much!! Cheers! –  user3201 Feb 15 '11 at 23:18
    
is is possible to make each show in a separate post ? –  mireille raad Feb 15 '11 at 23:22
    
@mireille raad: Yes its possible you can change the function to use wp_insert_post and that will fatch your tweets into posts. –  Bainternet Feb 15 '11 at 23:53
    
This looks like what i've been searching for, many thanks. Where do I add the wp_insert_post to ensure the separate tweets are added as posts? –  user12131 Jan 17 '12 at 1:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.