最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - WordPress transient not working with WP Engine

programmeradmin0浏览0评论

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, ".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, ".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']);
?>

It writes to the database:

.. 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?

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/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/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']);
?>

It writes to the database:

.. 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?

Share Improve this question asked Nov 22, 2019 at 21:23 user155484user155484 9
  • Since this issue only happens on WP-Engine, I would highly recommend contacting their support team. They're the best people to investigate and will have access to adjust any configuration needed. – WebElaine Commented Nov 22, 2019 at 21:39
  • Unfortunately, it looks like they don't handle code issues (which is what their claiming) - It has to be a caching issue.. hmm. – user155484 Commented Nov 22, 2019 at 21:48
  • In the else, shouldn't there be a $array = $cached_result; ? Why the return there? – Sally CJ Commented Nov 22, 2019 at 22:03
  • 1 json_decode( $cached_result, true ) should work. – Sally CJ Commented Nov 23, 2019 at 1:56
  • 1 @SallyCJ, FIXED! Worked like a champ, thanks so much! Post it as the answer if you'd like so I can upvote. – user155484 Commented Nov 23, 2019 at 2:04
 |  Show 4 more comments

1 Answer 1

Reset to default 1

So I don't know why the return $cached_result; in the else, but if that's intentional, then you should probably do:

return json_decode( $cached_result, true );

Because you're caching the response body (which is a JSON string) and you're reading the non-cached result like so: $array = json_decode($result, true);.

But if that return was a mistake, then this part:

<?php else: ?>
    <?php return $cached_result; ?>
<?php endif; ?>

Should be written as:

<?php else: ?>
    <?php $array = json_decode( $cached_result, true ); ?>
<?php endif; ?>
发布评论

评论列表(0)

  1. 暂无评论