For some reason, when we we're on another server (Tierpoint) everything worked fine and we recently switched over to WP Engine and the transients that I was using for my module have stopped working. Below, I will go into detail on both without and with transients.
WITHOUT Transients:
<?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/v5.0/". get_option('insta_id') ."/media?fields=media_url,permalink,username,media_type,thumbnail_url&access_token=". get_option('insta_accesstoken'));
$result = curl_exec($curl);
$array = json_decode($result, true);
?>
<?php
/* Loop through the array and only pull API fields */
$mediaUrls = array_map(function($entry) {
return [
'media_url' => $entry['media_url'],
'permalink' => $entry['permalink'],
'username' => $entry['username'],
'media_type' => $entry['media_type'],
'thumbnail_url' => !empty($entry['thumbnail_url']) ? $entry['thumbnail_url'] : ""
];
}, $array['data']);
?>
Media loads on every refresh of the website (Other html elements have not been included) - Works great:
WITH Transients:
<?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/v5.0/". get_option('insta_id') ."/media?fields=media_url,permalink,username,media_type,thumbnail_url&access_token=". get_option('insta_accesstoken'));
$result = curl_exec($curl);
$array = json_decode($result, true);
set_transient('instagram', $result, 8 * HOUR_IN_SECONDS);
?>
<?php else: ?>
<?php return $cached_result; ?>
<?php endif; ?>
<?php
/* Loop through the array and only pull API fields */
$mediaUrls = array_map(function($entry) {
return [
'media_url' => $entry['media_url'],
'permalink' => $entry['permalink'],
'username' => $entry['username'],
'media_type' => $entry['media_type'],
'thumbnail_url' => !empty($entry['thumbnail_url']) ? $entry['thumbnail_url'] : ""
];
}, $array['data']);
?>
.. but all that I get back is a blank screen as shown below:
Is this a caching issue? Am I not doing something right with the transient?
else
, shouldn't there be a$array = $cached_result;
? Why thereturn
there?json_decode( $cached_result, true )
should work.