As WordPress experts with years of experience optimizing site performance, we know the official Twitter follow button is convenient—but it loads external scripts that can slow your site. For a lightweight alternative, this guide shows you how to fetch and display your Twitter follower count as plain text using the Twitter API v1.1. We'll create a Twitter app, implement caching for speed, and integrate it seamlessly.
Here's the plan: Register a Twitter app to access the API securely, retrieve the follower count, cache it to minimize requests, and output it anywhere on your site. Let's dive in.
First, create a Twitter app. Head to the Twitter Developer Portal and sign in with your Twitter account. Click "Create an app".

Fill in the details: Use your site's name for the app name (e.g., your blog title), add a brief description, and enter your WordPress site URL (e.g., https://www.wpbeginner.com) in both the Website and Callback URL fields. Hit "Create your Twitter application".
Next, on your app's page, click "Create my access token". Confirm the notification that it's generated.
Grab your Consumer Key (API Key) and Consumer Secret (API Secret Key) from the app dashboard—these power the API calls.
Copy the PHP function below into your theme's functions.php file or a custom plugin. Replace YOUR_CONSUMER_KEY and YOUR_CONSUMER_SECRET with your actual values. This code handles OAuth, fetches the count, and caches it for 1 hour using WordPress transients for optimal performance.
function getTwitterFollowers( $screenName = 'wpbeginner' ) {
$consumerKey = 'YOUR_CONSUMER_KEY';
$consumerSecret = 'YOUR_CONSUMER_SECRET';
$token = get_option( 'cfTwitterToken' );
$numberOfFollowers = get_transient( 'cfTwitterFollowers' );
if ( false === $numberOfFollowers ) {
if ( ! $token ) {
$credentials = $consumerKey . ':' . $consumerSecret;
$toSend = base64_encode( $credentials );
$args = array(
'method' => 'POST',
'httpversion' => '1.1',
'blocking' => true,
'headers' => array(
'Authorization' => 'Basic ' . $toSend,
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
),
'body' => array(
'grant_type' => 'client_credentials'
)
);
add_filter( 'https_ssl_verify', '__return_false' );
$response = wp_remote_post( 'https://api.twitter.com/oauth2/token', $args );
$keys = json_decode( wp_remote_retrieve_body( $response ) );
if ( $keys ) {
update_option( 'cfTwitterToken', $keys->access_token );
$token = $keys->access_token;
}
}
$args = array(
'httpversion' => '1.1',
'blocking' => true,
'headers' => array(
'Authorization' => 'Bearer ' . $token
)
);
add_filter( 'https_ssl_verify', '__return_false' );
$api_url = 'https://api.twitter.com/1.1/users/show.json?screen_name=' . $screenName;
$response = wp_remote_get( $api_url, $args );
if ( ! is_wp_error( $response ) ) {
$followers = json_decode( wp_remote_retrieve_body( $response ) );
$numberOfFollowers = $followers->followers_count;
} else {
$numberOfFollowers = get_option( 'cfNumberOfFollowers' );
}
set_transient( 'cfTwitterFollowers', $numberOfFollowers, 1 * 60 * 60 );
update_option( 'cfNumberOfFollowers', $numberOfFollowers );
}
return $numberOfFollowers;
}To display the count, add this snippet to your theme template (e.g., sidebar.php or header.php):
<?php echo getTwitterFollowers( 'wpbeginner' ); ?> followersThat's it! Your site now shows real-time Twitter followers as text without performance hits. We've used this method on high-traffic sites to keep load times lightning-fast.
For more Twitter-WordPress integrations, try adding Twitter Cards or recent tweets. Follow @wpbeginner for tips.
Source: Adapted from Zvonko Biskup