Google Reviews as CPTs?

So I’m looking for the best way to implement dynamic Google Reviews on a site. I’m trying to find a solution that avoids me having to use a plugin that automatically imposes its own CSS styling on the reviews.

I’m wondering if there is a plugin that would periodically grab new Google Reviews and save them as custom post types that I could then loop through and style to my hearts content on Bricks?

ChatGPT says:

To integrate Google reviews into a WordPress website without using a plugin and map them to a Custom Post Type (CPT) using PHP, you can follow these steps:

  1. Get API Key: As mentioned before, obtain an API key from the Google Cloud Console and enable the Google Places API for your project.

  2. Fetch Reviews: Use PHP to fetch reviews from the Google Places API. You can use WordPress HTTP API functions like wp_remote_get to make HTTP requests. Here’s a simplified example:

$api_key = 'YOUR_API_KEY';
$place_id = 'PLACE_ID';

$response = wp_remote_get("https://maps.googleapis.com/maps/api/place/details/json?place_id={$place_id}&fields=reviews&key={$api_key}");

if ( is_wp_error( $response ) ) {
    $error_message = $response->get_error_message();
    echo "Something went wrong: $error_message";
} else {
    $body = wp_remote_retrieve_body( $response );
    $data = json_decode( $body );

    if ( isset( $data->result->reviews ) ) {
        $reviews = $data->result->reviews;
        // Process and map the reviews to your Custom Post Type (CPT)
        foreach ( $reviews as $review ) {
            // Create a new post of your CPT and save the review data
            $post_data = array(
                'post_title'   => $review->author_name,
                'post_content' => $review->text,
                'post_type'    => 'your_cpt_slug',
                // Additional post meta or taxonomies can be set here
            );
            $post_id = wp_insert_post( $post_data );

            // If you have custom fields or taxonomies, you can set them here
            // For example:
            // update_post_meta( $post_id, 'rating', $review->rating );
        }
    }
}
  1. Display Reviews: Once the reviews are mapped to your CPT, you can display them anywhere on your WordPress site using standard WordPress functions for querying and displaying posts (WP_Query, get_posts, etc.) tailored for your CPT.

  2. Cron Job (Optional): You may want to set up a cron job to periodically fetch and update the reviews to keep them fresh.

Ensure that you handle errors properly and follow Google’s usage policies when making API requests. Additionally, customize the code according to your CPT structure and requirements.