Svedr
Svedr

Reputation: 579

GET Requests in Wordpress Plugin extend the page load time for approx. 500-700ms

I've a few lines of code in my WordPress plugin that is supposed to first fetch the Tweets from a certain User and then return the oEmbed code for the received IDs. Although this plugin is only a few lines of code, it extends the load time of my page for 500-700ms. Is there someway I can improve this or either catch the results so it doesn't run on every refresh, since the plugin is loaded in the footer.

class TwitterFeed {

        public static function displayTweets( $count = 1) {

        $settings = array(
                'oauth_access_token' => "",
                'oauth_access_token_secret' => "",
                'consumer_key' => "",
                'consumer_secret' => ""
        );


        // Searches Tweets from the User
        $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
        $getfield = '?screen_name=ipmPress&count=' . $count . '';
        $requestMethod = 'GET';

        $twitter = new TwitterAPIExchange($settings);
        $tweets =  $twitter->setGetfield($getfield)
                ->buildOauth($url, $requestMethod)
                ->performRequest();

        $response = json_decode($tweets);

        // Gets Embed Response for the Collected Tweets
        foreach ($response as $tweet) {

            $url = 'https://api.twitter.com/1.1/statuses/oembed.json';
            $getfield = '?id=' . $tweet->id . '&omit_script=true&hide_media=true';
            $requestMethod = 'GET';

            $twitter = new TwitterAPIExchange($settings);
            $tweets =  $twitter->setGetfield($getfield)
                    ->buildOauth($url, $requestMethod)
                    ->performRequest();

            $response = json_decode($tweets);

            echo
            '<div class="large-4 medium-6 column">' .
                $response->html .
            '</div>';

        }

    }
}

Upvotes: 0

Views: 36

Answers (1)

Stanimir Stoyanov
Stanimir Stoyanov

Reputation: 1916

You should use WordPress Transients API to store the response.You can check the time distance between few tweets and use it for transient expiration time. Of course you can save the feed for 12 hours, but it's not recommended, I make new request every 30 minutes, which is normal. Here is an example using your code:

public static function displayTweets( $count = 1) {
    $key = "twitter-transient";

    $cached = get_transient($key);

    if (!$cached) {

        $settings = array(
          'oauth_access_token' => "",
          'oauth_access_token_secret' => "",
          'consumer_key' => "",
          'consumer_secret' => ""
        );


        // Searches Tweets from the User
        $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
        $getfield = '?screen_name=ipmPress&count=' . $count . '';
        $requestMethod = 'GET';

        $twitter = new TwitterAPIExchange($settings);
        $tweets =  $twitter->setGetfield($getfield)
          ->buildOauth($url, $requestMethod)
          ->performRequest();

        $response = json_decode($tweets);

        set_transient($key, $response, 1800);
        $cached = $response;
    }

    // Gets Embed Response for the Collected Tweets
    foreach ($cached as $tweet) {

        $url = 'https://api.twitter.com/1.1/statuses/oembed.json';
        $getfield = '?id=' . $tweet->id . '&omit_script=true&hide_media=true';
        $requestMethod = 'GET';

        $twitter = new TwitterAPIExchange($settings);
        $tweets =  $twitter->setGetfield($getfield)
            ->buildOauth($url, $requestMethod)
            ->performRequest();

        $response = json_decode($tweets);

        echo
        '<div class="large-4 medium-6 column">' .
          $response->html .
        '</div>';

    }
}

Upvotes: 2

Related Questions