Interacting with Twitter API in WordPress: How to get your favorite tweets
First of all we need Consumer Key and Consumer Secret, to obtain both of them, you have to create your own app on apps.twitter.com. It should take no more than 10 minutes.
OAuth with Bearer Token
Previously I wanted to create a special tool for you where you can generate your Bearer token but changed my mind and decided to share the code with you.
$key = ''; // Consumer Key (API Key) $secret = ''; // Consumer Secret (API Secret) $response = wp_remote_post( 'https://api.twitter.com/oauth2/token', array( 'method' => 'POST', 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( urlencode( $key ) . ':' . $secret ), 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8' ), 'body' => 'grant_type=client_credentials' ) ); $body = json_decode( $response['body'] ); echo $body->access_token; // that's your Bearer token
You have to remember one thing – you shouldn’t generate your Bearer token each time because it doesn’t have expiration period.
Just copy your token somewhere and let’s continue to the next step.
Connect to Twitter and Get Favorite Tweets
Please note, that the endpoint from the below code
1.1/favorites/list.json displays only tweets that were marked by a specific user as favorite (the heart button clicked).
For more endpoints please visit Twitter API reference.
$args = array( 'headers' => array( 'Authorization' => 'Bearer ' . $token // that's our token from the previous step ) ); $response = wp_remote_get( add_query_arg( array( 'count' => 10, // how much to display 'screen_name' => 'rudrastyh', // favorites of user @rudrastyh 'include_entities' => false ), 'https://api.twitter.com/1.1/favorites/list.json' ), $args ); // more params in Twitter docs https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/get-favorites-list // response body contains the json-encoded array of tweet objects $tweets = json_decode( $response['body'] ); $tweets_html = ''; foreach( $tweets as $tweet ) : $tweets_html .= '<div id="tweet-' . $tweet->id . '" class="tweet"> <div class="tweet_body">' . $tweet->text . '</div> <div class="tweet_head"> <img width="36" height="36" src="' . $tweet->user->profile_image_url_https . '" /> <strong>' . $tweet->user->name . ' </strong> <span>@' . $tweet->user->screen_name . '</span> </div> <div class="tweet_meta"> <span>' . date( 'H:i A - j M Y', strtotime( $tweet->created_at ) ) . '</span> </div> </div>'; endforeach; echo $tweets_html;
And of course I encourage you to use transient cache (
set_transient functions), the latest example you can find in this post.
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Please excuse this basic question, I understand how the above code works, but where do those files go inside a wordpress install, and how do you integrate the results in your theme?
You can use this code in any file where you would like to print the result.