Use WordPress Transient to show cURL

So there is a small issue that I’m running into that I can’t seem to figure out.

When i set set_transient(‘instagram’, ‘test’, 900); it returns the ‘test’ as the _transient_instagram in the database
enter image description here

When i set set_transient(‘instagram’, $cached_result, 900); it returns an empty value in the database instead of ‘instagram’.
enter image description here

What I’m doing:

Check the $cached_result = empty
If empty, do the cURL call and set
the transient
else if it’s not empty, return the cached result

What I have so far:

<?php
$cached_result = get_transient(‘instagram’);
if(empty($cached_result)): ?>
<?php
/* Call the cURL request to pull in Instagram images */
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, “https://graph.facebook.com/v4.0/”. get_option(‘insta_id’) .”/media?fields=media_url,permalink,username&access_token=”. get_option(‘insta_accesstoken’));
$result = curl_exec($curl);
$array = json_decode($result, true);
set_transient(‘instagram’, $cached_result, 900);
?>
<?php else: ?>
<?php return $cached_result; ?>
<?php endif; ?>

Read more here:: Use WordPress Transient to show cURL

Leave a Reply

Your email address will not be published. Required fields are marked *