4

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!

2
  • 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? Feb 15, 2011 at 21:53
  • It's preferable to import them into the database, but the first option is good too.
    – user3201
    Feb 15, 2011 at 21:59

1 Answer 1

11

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.

3
  • is is possible to make each show in a separate post ? Feb 15, 2011 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, 2011 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, 2012 at 1:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.