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.

How can I show my latest tweet in my wordpress blog?

in functions.php not use any plugin

share|improve this question
add comment

3 Answers

function get_twitter_feed(){

$userID = 'ImOttoman';  //your twiter username

$url = 'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=$userID&count=1';

$xml = simplexml_load_file($url) or die("could not connect");

       foreach($xml->status as $status){
       $text = $status->text;
       }
       echo $text;
}
add_filter('wp_head','get_twitter_feed');

You never said where you want it so this just pops it into the head (not a great idea), and is a bad example lol.

Instead you should use the HTTP API and cache the results for at least 10 minutes.

Also you should just use a plugin for more features because there is NO difference between a plugin and functions.php.

share|improve this answer
add comment

Below is the function I use to fetch the latest tweet. The benefits of this is:

  • using HTTP API
  • using transient to cache
  • using recent Twitter API

For more information, please read this tutorial:

function yourprefix_latest_tweet( $username )
{
    $key = "tweet-{$username}";
    if ( false != ( $tweet = get_transient( $key ) ) )
        return $tweet;
    $args = array(
        'screen_name' => $username,
        'count'       => 1,
        'trim_user'   => 1,
    );
    $url = 'https://api.twitter.com/1/statuses/user_timeline.json';
    $url = add_query_arg( $args, $url );
    $request = wp_remote_get( $url );
    if ( is_wp_error( $request ) )
        return;
    $body = wp_remote_retrieve_body( $request );
    $body = json_decode( $body );
    $tweet = $body[0]->text;
    // Make links
    $tweet = utf8_decode( $tweet );
    $tweet = preg_replace('@(https?://([-\w\.]+)+(d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>',  $tweet );
    $tweet = preg_replace("#(^|[\n ])@([^ \"\t\n\r< ]*)#ise", "'\\1<a href=\"http://www.twitter.com/\\2\" >@\\2'", $tweet);  
    $tweet = preg_replace("#(^|[\n ])\#([^ \"\t\n\r< ]*)#ise", "'\\1<a href=\"http://hashtags.org/search?query=\\2\" >#\\2'", $tweet);
    set_transient( $key, $tweet, 3600 );
    return $tweet;
}
share|improve this answer
add comment

To do this without a plugin is pretty simple, you can utilise built in WordPress functionality such as a combination of short codes and widgets, or simply short codes placed within content if you prefer.

Using the WordPress functions file to create short codes is easy and a detailed tutorial can be found here http://wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/

If you were to combine short code creation with a decent latest tweets tutorial then you will easily accomplish your goal, if you are unable to follow the short code tutorial to achieve this then you are probably better off using a plugin until such times as you understand PHP enough that you can.

share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.