Simple wordpress function to use twitter api oauth easily, get twitter api access token using oauth in wordpress with example.

How To Use Twitter API OAuth

Using wp_remote_post() function, we will use twitter api oauth, and get twitter access token.

Create Twitter App

Firstly, go to Twitter Apps, and create new app, we need “Consumer Key” and “Consumer Secret” only, go to this tutorial and read “Consumer Key And Consumer Secret” section.

Twitter API OAuth Function

This is function to use twitter oauth and get access token (enter your consumer key and consumer secret):

function WPTime_Twitter_API_OAuth(){

    /* Function By Qassim Hassan, wp-time.com */

    if( !get_option('my_twitter_access_token') ) {

        $consumer_key = 'enter_your_consumer_key';
        $consumer_secret = 'enter_your_consumer_secret';

        $base64 = base64_encode($consumer_key.':'.$consumer_secret);

        $oauth_args = array(

            "headers" => array(
            "Authorization" => "Basic $base64",
            "Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8",
            "Accept-Encoding" => "gzip",
        ),

        "body" => array( "grant_type" => "client_credentials" ),

        );

        $response = wp_remote_post('https://api.twitter.com/oauth2/token', $oauth_args);
        $result = json_decode( wp_remote_retrieve_body($response), true );

        if( !empty($result['access_token']) ){
            $access_token = $result['access_token'];
        }else{
            $access_token = null;
        }

        update_option('my_twitter_access_token', $access_token);

    }

}

WPTime_Twitter_API_OAuth(); // Call function.

Now your access token will be saved in ‘my_twitter_access_token’ option, if you want to display it, use this function:

get_option('my_twitter_access_token');

Anyway, now we will learn how to use access token to get data from twitter api, for example get twitter profile data.

Get Twitter Profile Data

Now using wp_remote_get() function, we will get twitter profile data using our access token:

function get_twitter_profile_data($screen_name){
    /* Function By Qassim Hassan, wp-time.com */

    $access_token = get_option('my_twitter_access_token'); // your access token.

    if( !$access_token ){ // if no access token
        return false;
    }

    if( empty($screen_name) ){
        return false;
    }

    $args = array(
                'headers' => array(
                'Authorization' => 'Bearer '.$access_token.'',
                'Accept-Encoding' => 'gzip'
            )
    );

    $response = wp_remote_get("https://api.twitter.com/1.1/users/show.json?screen_name=$screen_name", $args);

    $results = json_decode( $response['body'], true );

    return $results;
}

Twitter profile function:

function twitter_profile($screen_name){
    /* Function By Qassim Hassan, wp-time.com */

    // Read example result: https://dev.twitter.com/rest/reference/get/users/show

    if( empty($screen_name) ){
        return false;
    }

    $profile = get_twitter_profile_data($screen_name); // get profile data

    $followers_count = $profile['followers_count']; // get followers count

    $following_count = $profile['friends_count']; // get following count

    $profile_image = str_replace('_normal', null, $profile['profile_image_url']); // get profile image link

    $tweets_count = $profile['statuses_count']; // get tweets count

    $name = $profile['name']; // get name

    ?>
        <h1><?php echo $name; ?></h1>
        <p><img src="<?php echo $profile_image; ?>"></p>
        <ul>
            <li>Tweets: <?php echo $tweets_count; ?></li>
            <li>Following: <?php echo $following_count; ?></li>
            <li>Followers: <?php echo $followers_count; ?></li>
        </ul>
    <?php

}

Now to get twitter profile data, call this function with username:


<?php twitter_profile('here_enter_twitter_username'); ?>

Now will be display twitter profile data.

To display twitter profile data in shortcode, use this code:

function get_twitter_profile($atts){
    /* Function By Qassim Hassan, wp-time.com */

    ob_start();

    if( !empty($atts['username']) ){
        $username = $atts['username'];
    }else{
        $username = 'twitter';
    }

    twitter_profile($username);

    return ob_get_clean();
}
add_shortcode('get_twitter_profile', 'get_twitter_profile');

Now in your post, use this shortcode:

[get_twitter_profile username="jack"]

Change “jack” to your username or any username on twitter.