Google Reviews Cron Setup

User
<style>
#google-reviews-container {}
        .reviews-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
        }
        .review-item {
            padding: 34px 29px 34px 38px;
            box-shadow: 4px 7px 10px 0px #0000000D;
            border-radius: 18px;
            background: #fff;
        }
        .review-header {
            display: flex;
            align-items: center;
            gap: 20px;
        }
        img.review-avatar {
            width: 68px;
            height: auto;
        }
        .authorName {
            margin-bottom: 0;
            font-size: 16px;
            line-height: 21px;
        }
        p.review-rating {
            margin-bottom: 0;
            color: #F9BF3B;
        }
        p.review-text {
            margin-top: 15px;
            color: #2C2C20;
        }
        .star {
            font-size: 21px;
            line-height: 17px;
            color: #ccc;
        }
        .star.filled {
            color: #FFD700;
            font-size: 21px;
            line-height: 17px;
        }
        .view-more-btn {
            display: block;
            margin: 20px auto;
            text-align: center;
            color: white;
            border: none;
            cursor: pointer;
            font-weight: 700;
        }
        #load-more-reviews {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 20px;
            font-size: 16px;
            font-weight: 500;
            font-family: "Poppins";
            color: #000;
            margin-top: 45px;
        }
</style>

';

$output .= '';
$output .= '';

foreach ($reviews as $index => $review) {
$hidden = $index >= 4 ? 'style="display:none;"' : '';
$stars = str_repeat('⭐', (int)$review['rating']);

$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '' . esc_html($review['author_name']) . '';
$output .= '' . $stars . '';
$output .= '';
$output .= '' . esc_html($review['text']) . '';
$output .= '' . esc_html($review['relative_time_description']) . '';
$output .= '';
}

$output .= ''; // Close reviews-grid
$output .= ' Bekijk meer reviews

';

$output .= '

<script>
document.getElementById("load-more-reviews").addEventListener("click", function() {
            document.querySelectorAll(".review-item[style='display:none;']").forEach(function(el) {
                el.style.display = "block";
            });
            this.style.display = "none";
        });
</script>

';

return $output;
}
add_shortcode('google_reviews', 'display_google_reviews_shortcode');
activate cron schedule activate when theme activate

Assistant
Your code is almost complete and functional, but to ensure the cron event activates when the theme is activated (as mentioned at the end of your message), you'll need to hook into the `after_switch_theme` action. Here's how to integrate that final piece:

### ✅ Add Cron Event Activation on Theme Activation

Place this at the bottom of your theme’s `functions.php` or in your custom plugin file:

// Schedule the cron job when theme is activated
add_action('after_switch_theme', function () {
    if (!wp_next_scheduled('update_google_place_details')) {
        wp_schedule_event(time(), 'three_days', 'update_google_place_details');
    }
});

This ensures the cron job is created whenever your theme is activated.

### ✅ Optional: Unschedule Cron on Theme Switch (Cleanup)

If you want to **clean up** the scheduled event when switching away from the theme:

add_action('switch_theme', function () {
    $timestamp = wp_next_scheduled('update_google_place_details');
    if ($timestamp) {
        wp_unschedule_event($timestamp, 'update_google_place_details');
    }
});

Let me know if you'd like:
– Pagination with AJAX instead of just showing all hidden reviews
– Error logging in `getPlaceDetails()` for better debugging
– A WP Admin page to manually refresh reviews

You're on a solid track!

Leave a Comment

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