Loading...

How to Fetch Latest Tweets with Twitter API 1.1 in Wordpress

Twitter is ranked at number 13th in the world wide ranking of the entire websites. Lately twitter has been improving its functionality, appearance and now it has updated it's API from 1.0 to 1.1 since quite a while.

Many People are having issues with the upgrade of the twitter API because the older version is no more functional and the the new API requires OAuth before one can access twitter account data with the API. This tutorial will guide you step by step how you can connect an authenticated connection with twitter using OAuth fetch latest tweets.

1) Create a Developer Account for twitter

The very first step is to setup your existing twitter account as developer account for twitter. If you are not a developer, it is alright you can just follow up with this tutorials and things will cool down for you as you proceed. So you can set up your existing twitter account as a developer account here. Setting up a developers account is free and is necessary to use twitter API 1.1

2) Create a Twitter Application

Now you need to create your own app that will fetch latest tweets from your twitter timeline. Nothing to panic still as it is very easy to create an app. Once you are logged into your twitter developer account, next you will have to click here to go create a new application page. Now simply click "Create a new application" button.


3) Fill in the New Application form and get the Unique Access Tokens

Now you need to fill in the required details and then submit the form and click on "create my access token".  Twitter will provide you with your unique access keys and access tokens that include:

  • The consumer key
  • The consumer secret
  • The access token
  • The access token secret
You can get more information about these tokens here.


4) Change Access Level

You can change the access level that you want to assign to the application that you just created by going into settings tab.

Either can be read-only or read and write. For now, lets set it to read and write and click the update button to save the settings.


Now that your app is all set and is ready, lets now jump to the fun stuff.

We need to install and activate a wordpress plugin now. You can get the twitter feed plugin file here.
This plugin is basically a PHP helper library that will help us access functions that will authenticate us and help us retrieve data from the twitter profile.

Once the plugin has been activated, we can create a new object that will access the functions inside the library class. Lets assume I want the tweets to be shown in my footer area, I will have to appearance > Editor to edit footer.php

Next I copy the following code and paste it where you want the latest fetched tweets to be rendered:

<?php
echo '<ul>';

// Setting our Authentication Variables that we got after creating an application
$settings = array(
    'oauth_access_token' => "YOUR_ACCESS_TOKEN_HERE",
    'oauth_access_token_secret' => "YOUR_ACCESS_SECRET_TOKEN_HERE",
    'consumer_key' => "YOUR_CONSUMER_KEY_HERE",
    'consumer_secret' => "YOUR_CONSUMER_SECRET_HERE"
);

// We are using GET Method to Fetch the latest tweets.
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';

// Set your screen_name to your twitter screen name. Also set the count to the number of tweets you want to be fetched. Here we are fetching 5 latest tweets.
$getfield = '?screen_name=YOUR_TWITTER_SCREEN_NAME&count=5';
$requestMethod = 'GET';

// Making an object to access our library class
$twitter = new TwitterAPIExchange($settings);
$store = $twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest();
// Since the returned result is in json format, we need to decode it             
  $result = json_decode($store);

// After decoding, we have an standard object array, so we can print each tweet into a list item.
  $multi_array = objectToArray($result);
 foreach($multi_array as $key => $value ){

// printing each tweet wrapped in a <li> tag
 echo '<li>'.$value["text"].'</li>';

 }
echo '</ul>'; ?>

That is it, you are now able to successfully pull latest feeds within your wordpress site. If you have any problem integrating these twitter feeds, Please feel free to ask question in the comments below.

Wordpress Tricks 1207054134106740504

Post a Comment

  1. Hi
    I am a little unsure where to place the Twitter Feed Plugin, in the plugin's folder?

    thanks

    ReplyDelete
  2. @Thandi Viljoen

    Yes we need to create a new plugin folder in wp-content/plugins/YOUR_PLUGIN_FOLDER_NAME, give it a unique name and place the plugin inside, then simply activate it from the admin panel.

    ReplyDelete
  3. There a way to customize this so it only pulls certain hashtags?

    ReplyDelete
  4. Not working. I get:

    Fatal error: Call to undefined function objectToArray() in ....

    ReplyDelete
  5. Is there any way to ensure that links are clickable (i.e. wrap in href tags?) Thanks, works great.

    ReplyDelete
  6. hi farsab the php file you gave cannot be installed..wordpress always gives me an error...i had to manually copy paste it into the plugins directory to install and activate it.

    1. can you modify your plugin so that it makes the usernames, hashtags, and the links clickable with nofollow tag to them
    2. displays the time when the tweet was made. like '2 days ago'

    a million thanks for this plugin...had been looking for it since ages as the twitter moved its API to 1.1...please update the plugin...it will be a huge help for all of us...thanks again! i will even write a review about it. :)

    ReplyDelete
  7. How do I get the links in tweets to work?

    ReplyDelete
  8. Thank you, it works perfectly!

    ReplyDelete
  9. Hi ! Thank you so much for such an amazing piece of code that I've searching for months (no jokes! ^^).

    I still have one question : it currently outputs plain text. Is there a way in the "twitter object configuration", to output as html (links included) ?

    Thks a lot !!

    ReplyDelete
    Replies
    1. Wrote a little function to link add links.

      check it out here -> http://pastebin.com/3MAcByqd

      should link any links in the tweet as well as hash tags and usernames mentioned.

      Delete
  10. for those who want to add link -and the owner of this article- , I worked on it and it is quite simple to do this.


    $value['text'] -- this gets the tweet. so there are some other values in the array then.

    $value['id_str'] -- this gets tweet ID.

    The rest is easy.. just write your twitter link and add status + $value['id_str'] at the end;

    example

    http://twitter.com/aliogul/status/$value['id_str']

    thats it.. good luck



    ReplyDelete

Please Avoid Spamming. Comments will be moderated before they are published.

emo-but-icon

Home item

Follow by Email

WPReads.com aims to provide everything that can help you master wordpress, I am Farsab, a creative webdev who likes to spread knowledge through writing on different topics such as web development concepts, tutorials, SEO, CSS and much more . Feel free to

Popular Posts