WordPress: How to Create a Shortcode To Publish Your Google Calendar Booking Button

Google Calendar’s appointment scheduling feature allows businesses, freelancers, and professionals to streamline bookings without needing third-party scheduling software. However, Google doesn’t provide an easy way to embed a booking button directly into a WordPress site. Instead, users must insert a custom script or link to the external page.

To make this process seamless, a simple WordPress plugin can enable you to add a Google Calendar booking button anywhere on your website using a shortcode. This plugin ensures visitors can schedule appointments directly from your site with just one click.

Why Use Google Calendar for Scheduling?

Google Calendar’s appointment scheduling is a built-in feature that allows users to create dedicated time slots for meetings, client consultations, or other bookings. Some of the benefits include:

Despite these advantages, Google doesn’t offer a built-in WordPress integration. This is where the Google Calendar Button Plugin comes in handy.

Introducing the Google Calendar Button Plugin

The Google Calendar Button Plugin is a lightweight WordPress plugin that allows you to insert a scheduling button using a shortcode. You can use your All Appointment URL or any of your Single Event URLs for the link. Once activated, here’s an example:

Here’s how to format the shortcode using the URL for your calendar link:

[googlecalendar link="Your_Google_Calendar_Link" color="#YourColor"]Button Text[/googlecalendar]

The plugin supports:

If no button text is provided, the default text will be “Book an Appointment.”

How to Install the Google Calendar Button Plugin

Follow these steps to install and activate the plugin on your WordPress site:

  1. Create a new folder in the wp-content/plugins/ directory and name it google-calendar-button.
  2. Create a new file inside that folder and name it google-calendar-button.php.
  3. Copy and paste the following code into the google-calendar-button.php file.
  4. Save the file and activate the plugin from the WordPress admin dashboard.

Google Calendar Button Plugin Code

<?php
/**
 * Plugin Name: Google Calendar Button
 * Plugin URI:  https://dknewmedia.com
 * Description: A simple shortcode to insert a Google Calendar appointment scheduling button.
 * Version:     1.0
 * Author:      Douglas Karr
 * Author URI:  https://dknewmedia.com
 * License:     GPL-2.0+
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

function google_calendar_scheduler_shortcode($atts, $content = null) {
    $atts = shortcode_atts(
        array(
            'link'  => '', // You can optionally default your link here
            'color' => '#039BE5', // Default color (Google blue)
        ),
        $atts,
        'googlecalendar'
    );

    if (empty($atts['link'])) {
        return '<p style="color: red; font-weight: bold;">Error: No Google Calendar link provided in the shortcode.</p>';
    }

    // Default label if no custom text is provided
    $button_label = !empty(trim($content)) ? esc_html($content) : 'Book an Appointment';

    ob_start();
    ?>
    <!-- Google Calendar Appointment Scheduling begin -->
    <link href="https://calendar.google.com/calendar/scheduling-button-script.css" rel="stylesheet">
    <script src="https://calendar.google.com/calendar/scheduling-button-script.js" async></script>
    <script>
    (function() {
      var target = document.currentScript;
      window.addEventListener('load', function() {
        calendar.schedulingButton.load({
          url: '<?php echo esc_url($atts['link']); ?>',
          color: '<?php echo esc_attr($atts['color']); ?>',
          label: '<?php echo esc_html($button_label); ?>',
          target,
        });
      });
    })();
    </script>
    <!-- end Google Calendar Appointment Scheduling -->
    <?php
    return ob_get_clean();
}

add_shortcode('googlecalendar', 'google_calendar_scheduler_shortcode');

How to Use the Shortcode

Once the plugin is activated, you can add a Google Calendar booking button anywhere on your WordPress site using the shortcode. Here’s a basic example:

[googlecalendar link="https://calendar.google.com/calendar/appointments/YourAppointmentLinkHere"]
[/googlecalendar]

Custom Button Text & Color

[googlecalendar link="https://calendar.google.com/calendar/appointments/YourAppointmentLinkHere" color="#ff5733"]Schedule Now[/googlecalendar]

Error Handling (No Link Provided)

If no link is provided in the shortcode, the following error message will be displayed:
Error: No Google Calendar link provided in the shortcode.

Google Calendar is a powerful tool for scheduling meetings, but embedding a booking button in WordPress can be tricky. The Google Calendar Button Plugin solves this problem by allowing users to insert a scheduling button using a simple shortcode.

This plugin is the perfect solution if you’re looking for a simple and effective way to integrate Google Calendar appointments into your WordPress site.

Exit mobile version