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.

How to get your twitter Consumer key and secret

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).

Favorite Tweet

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 (get_transient() and set_transient functions), the latest example you can find in this post.

Misha Rudrastyh

Misha Rudrastyh

I develop websites since 2008, so it is total of 13 years of experience, oh my gosh. Most of all I love love love to create websites with WordPress and Gutenberg, some ideas and thoughts I share throughout my blog.

Need some developer help? Contact me

Follow me on Twitter