s

Regardless of niche, adding a calendar plugin to your WordPress site is a great way to improve user engagement and retention. Creating such a plugin doesn’t take much effort because there are lots of cloud-based calendaring packages available today. Google Calendar is the most popular of them all, and it offers a free to use API.

In this tutorial, I’ll show you how to use the Google Calendar API to quickly create a WordPress calendar plugin that can display events from any publicly accessible Google calendar.

Prerequisites

To be able to make the most of this tutorial, you’ll need:

  • PHP 7.0.32 or higher
  • WordPress 4.4.2 or higher
  • Composer 1.8.0 or higher
  • a Google account

1. Creating a Google Project

You can use the Google Calendar API only after you’ve created an appropriately configured project on the Google Cloud Platform console. So log in to the console and, in the dashboard, press the Create button.

Dashboard of the Google Cloud Platform console

In the form that’s shown next, give a name to your project, change its ID if you want to, and press the Create button.

Project creation form

Once the project is ready, open the API library, select the Google Calendar API card, and press the Enable button to activate the API.

Calendar API home

2. Creating a Service Account

Unlike most other Google APIs, the Google Calendar API doesn’t allow you to authenticate your requests using an API key. Instead, it expects an OAuth 2.0 authorization token.

To be able to generate such a token from the server you’re running WordPress on, you’ll need a Google service account. So go to the Credentials section, press the Create Credentials button, and select the Service account key option.

Credentials creation page

In the next screen, choose the New service account option and type in a name for the service account. In the Role field, select the Role Viewer option. This will give the service account read-only access to all your Google calendars.

Service account creation form

Once you press the Create button, you’ll receive a JSON file containing a private key and a client ID. Save the file because you’ll need it later.

3. Creating a WordPress Plugin

To start creating the WordPress plugin, create a new directory called my-gcal-plugin inside the wp-content/plugins/ directory of your WordPress installation. You are free to use any other name for the directory. However, if you plan to share the plugin in the future, make sure that the name is unique in order to avoid conflicts with other plugins.

Inside the directory, create a PHP file named my-gcal-plugin.php and open it using a text editor. 

For WordPress to recognize your plugin, this file needs to have a valid header comment. At the very least, the comment must specify the name of your plugin. However, I suggest including a short description and a version number too. So add the following code inside the file:

No comments so far.

Leave a Reply