Add Google Reviews Widget to WordPress Easily

How to add google reviews widget to wordpress; Best google reviews plugin for wordpress; Embed google reviews on wordpress site; Free google reviews widget for wordpress; Display google reviews on wordpress; Integrate google reviews with wordpress; Google reviews widget setup for wordpress; Wordpress plugin for google reviews; Show google reviews on wordpress website; Install google reviews widget wordpress;

Explanation

To show Google reviews on your WordPress site, this code helps you add a widget that displays them. Here's how it works:

  • Script Loading: The code includes a script for the Google Reviews Widget. It loads only on the front end of your site, not in the admin area.
  • Shortcode: You can use a shortcode [google_reviews place_id="YOUR_PLACE_ID"] to display the reviews. Just replace YOUR_PLACE_ID with your actual Google Place ID.
  • Settings Page: There's a settings page in your WordPress admin where you can save your Google Place ID. This makes it easier to manage without editing the shortcode each time.

When you use the shortcode, the widget will appear wherever you place it on your site, showing reviews from the specified Google Place ID. Make sure to enter a valid Place ID in the settings to see the reviews correctly.

Code

<?php
// Function to enqueue Google Reviews Widget script
function wp_dudecom_enqueue_google_reviews_script() {
    if ( ! is_admin() ) {
        wp_enqueue_script( 'google-reviews-widget', 'https://path-to-google-reviews-widget.js', array(), null, true );
    }
}
add_action( 'wp_enqueue_scripts', 'wp_dudecom_enqueue_google_reviews_script' );

// Shortcode to display Google Reviews Widget
function wp_dudecom_display_google_reviews_widget( $atts ) {
    // Ensure the attributes are sanitized
    $atts = shortcode_atts(
        array(
            'place_id' => '', // Google Place ID
        ),
        $atts,
        'google_reviews'
    );

    // Check if Place ID is provided
    if ( empty( $atts['place_id'] ) ) {
        return '<p>Please provide a valid Google Place ID.</p>';
    }

    // Output the Google Reviews Widget
    ob_start();
    ?>
    <div id="google-reviews-widget" data-place-id="<?php echo esc_attr( $atts['place_id'] ); ?>"></div>
    <script>
        // Initialize the Google Reviews Widget
        document.addEventListener('DOMContentLoaded', function() {
            if (typeof GoogleReviewsWidget !== 'undefined') {
                GoogleReviewsWidget.init({
                    placeId: '<?php echo esc_js( $atts['place_id'] ); ?>',
                    container: '#google-reviews-widget'
                });
            }
        });
    </script>
    <?php
    return ob_get_clean();
}
add_shortcode( 'google_reviews', 'wp_dudecom_display_google_reviews_widget' );

// Function to add settings page for Google Reviews Widget
function wp_dudecom_google_reviews_settings_page() {
    add_options_page(
        'Google Reviews Settings',
        'Google Reviews',
        'manage_options',
        'google-reviews-settings',
        'wp_dudecom_google_reviews_settings_page_html'
    );
}
add_action( 'admin_menu', 'wp_dudecom_google_reviews_settings_page' );

// HTML for the settings page
function wp_dudecom_google_reviews_settings_page_html() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    if ( isset( $_POST['wp_dudecom_google_reviews_nonce'] ) && wp_verify_nonce( $_POST['wp_dudecom_google_reviews_nonce'], 'wp_dudecom_google_reviews_save_settings' ) ) {
        update_option( 'wp_dudecom_google_reviews_place_id', sanitize_text_field( $_POST['wp_dudecom_google_reviews_place_id'] ) );
        echo '<div class="updated"><p>Settings saved.</p></div>';
    }

    $place_id = get_option( 'wp_dudecom_google_reviews_place_id', '' );
    ?>
    <div class="wrap">
        <h1>Google Reviews Settings</h1>
        <form method="post" action="">
            <?php wp_nonce_field( 'wp_dudecom_google_reviews_save_settings', 'wp_dudecom_google_reviews_nonce' ); ?>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Google Place ID</th>
                    <td>
                        <input type="text" name="wp_dudecom_google_reviews_place_id" value="<?php echo esc_attr( $place_id ); ?>" class="regular-text" />
                        <p class="description">Enter your Google Place ID to display reviews.</p>
                    </td>
                </tr>
            </table>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}
?>

Instructions

File Location: Add the code to your theme's functions.php file or create a custom plugin file.

Prerequisites: Ensure you have access to your WordPress admin dashboard and the ability to edit theme files or upload plugins.

Implementation Steps:

  1. Access Your WordPress Files:
    • Log in to your WordPress admin dashboard.
    • Navigate to Appearance > Theme Editor if you are editing the functions.php file, or use an FTP client to upload a custom plugin file.
  2. Add the Code:
    • Copy the provided code snippet.
    • Paste it into the functions.php file or your custom plugin file.
    • Save the changes.
  3. Configure the Google Place ID:
    • In your WordPress admin dashboard, go to Settings > Google Reviews.
    • Enter your Google Place ID in the provided field.
    • Click Save Changes to store your settings.
  4. Use the Shortcode:
    • In any post or page, add the shortcode [google_reviews place_id="YOUR_PLACE_ID"].
    • Replace YOUR_PLACE_ID with the Google Place ID you configured in the settings.
    • Publish or update the post/page to display the Google Reviews Widget.

If you need help with implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert assistance.

\n

Settings saved.

';\n }\n\n $place_id = get_option( 'wp_dudecom_google_reviews_place_id', '' );\n ?>\n
\n

Google Reviews Settings

\n
\n \n\n\n\n\n\n
Google Place ID\n \" class=\"regular-text\" />\n

Enter your Google Place ID to display reviews.

\n
\n \n
\n
\n ", "author": { "@type": "Person", "name": "WP-Dude.com" }, "datePublished": "2024-12-20T15:58:36+00:00", "dateModified": "2024-12-20T15:58:36+00:00" }, { "@type": "TechArticle", "@id": "https://www.wp-dude.com/code-snippet/add-google-reviews-widget-wordpress/#article", "headline": "Add Google Reviews Widget to WordPress Easily", "description": "To show Google reviews on your WordPress site, this code helps you add a widget that displays them. Here's how it works:\n\n\n Script Loading: The code includes a script for the Google Reviews Widget. It loads only on the front end of your site, not in the admin area.\n Shortcode: You can use a shortcode [google_reviews place_id=\"YOUR_PLACE_ID\"] to display the reviews. Just replace YOUR_PLACE_ID with your actual Google Place ID.\n Settings Page: There's a settings page in your WordPress admin where you can save your Google Place ID. This makes it easier to manage without editing the shortcode each time.\n\n\nWhen you use the shortcode, the widget will appear wherever you place it on your site, showing reviews from the specified Google Place ID. Make sure to enter a valid Place ID in the settings to see the reviews correctly.", "articleBody": "To show Google reviews on your WordPress site, this code helps you add a widget that displays them. Here's how it works:\n\n\n Script Loading: The code includes a script for the Google Reviews Widget. It loads only on the front end of your site, not in the admin area.\n Shortcode: You can use a shortcode [google_reviews place_id=\"YOUR_PLACE_ID\"] to display the reviews. Just replace YOUR_PLACE_ID with your actual Google Place ID.\n Settings Page: There's a settings page in your WordPress admin where you can save your Google Place ID. This makes it easier to manage without editing the shortcode each time.\n\n\nWhen you use the shortcode, the widget will appear wherever you place it on your site, showing reviews from the specified Google Place ID. Make sure to enter a valid Place ID in the settings to see the reviews correctly. [\"how to add google reviews widget to wordpress\", \"best google reviews plugin for wordpress\", \"embed google reviews on wordpress site\", \"free google reviews widget for wordpress\", \"display google reviews on wordpress\", \"integrate google reviews with wordpress\", \"google reviews widget setup for wordpress\", \"wordpress plugin for google reviews\", \"show google reviews on wordpress website\", \"install google reviews widget wordpress\"] To show Google reviews on your WordPress site, this code helps you add a widget that displays them. Here's how it works:\n\n\n Script Loading: The code includes a script for the Google Reviews Widget. It loads only on the front end of your site, not in the admin area.\n Shortcode: You can use a shortcode [google_reviews place_id=\"YOUR_PLACE_ID\"] to display the reviews. Just replace YOUR_PLACE_ID with your actual Google Place ID.\n Settings Page: There's a settings page in your WordPress admin where you can save your Google Place ID. This makes it easier to manage without editing the shortcode each time.\n\n\nWhen you use the shortcode, the widget will appear wherever you place it on your site, showing reviews from the specified Google Place ID. Make sure to enter a valid Place ID in the settings to see the reviews correctly.", "articleSection": "External Integrations", "datePublished": "2024-12-20T15:58:36+00:00", "dateModified": "2024-12-20T15:58:36+00:00", "author": { "@type": "Person", "name": "WP-Dude.com" }, "url": "https://www.wp-dude.com/code-snippet/add-google-reviews-widget-wordpress/", "wordCount": 352, "speakable": { "@type": "SpeakableSpecification", "cssSelector": ".post__content" }, "dependencies": "WordPress", "proficiencyLevel": "Beginner" }, { "@type": "HowTo", "@id": "https://www.wp-dude.com/code-snippet/add-google-reviews-widget-wordpress/#howto", "name": "Add Google Reviews Widget to WordPress Easily - How to", "description": "To show Google reviews on your WordPress site, this code helps you add a widget that displays them. Here's how it works:\n\n\n Script Loading: The code includes a script for the Google Reviews Widget. It loads only on the front end of your site, not in the admin area.\n Shortcode: You can use a shortcode [google_reviews place_id=\"YOUR_PLACE_ID\"] to display the reviews. Just replace YOUR_PLACE_ID with your actual Google Place ID.\n Settings Page: There's a settings page in your WordPress admin where you can save your Google Place ID. This makes it easier to manage without editing the shortcode each time.\n\n\nWhen you use the shortcode, the widget will appear wherever you place it on your site, showing reviews from the specified Google Place ID. Make sure to enter a valid Place ID in the settings to see the reviews correctly.", "step": [ { "@type": "HowToStep", "text": "File Location: Add the code to your theme's functions.php file or create a custom plugin file.\n\nPrerequisites: Ensure you have access to your WordPress admin dashboard and the ability to edit theme files or upload plugins.\n\nImplementation Steps:\n\n\n Access Your WordPress Files:\n \n Log in to your WordPress admin dashboard.\n Navigate to Appearance > Theme Editor if you are editing the functions.php file, or use an FTP client to upload a custom plugin file.\n \n \n Add the Code:\n \n Copy the provided code snippet.\n Paste it into the functions.php file or your custom plugin file.\n Save the changes.\n \n \n Configure the Google Place ID:\n \n In your WordPress admin dashboard, go to Settings > Google Reviews.\n Enter your Google Place ID in the provided field.\n Click Save Changes to store your settings.\n \n \n Use the Shortcode:\n \n In any post or page, add the shortcode [google_reviews place_id=\"YOUR_PLACE_ID\"].\n Replace YOUR_PLACE_ID with the Google Place ID you configured in the settings.\n Publish or update the post/page to display the Google Reviews Widget.\n \n \n\n\nIf you need help with implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert assistance." } ] } ] }