The NCSU Google Drive Integration plugin for WordPress allows users to integrate the WordPress media library with a folder in Google Drive. The files in the Google Drive folder appear in the WordPress media library as if they were regular uploaded files, but the content exists in Google Drive instead of on the WordPress web server. This allows users to utilize Google Drive as a storage medium to host large files instead of uploading them to WordPress and consuming disk space on the web server.
Installing the Plugin
To install the plugin, it is recommended to use the NC State Plugin Repository system: https://www.webtools.ncsu.edu/wp-updates/
See here for more information on using the NC State Plugin Repository system: https://github.ncsu.edu/ncstate-wordpress/ncsu-cthulhu#cthulhu
Configuring the Plugin and Linking to Google
By default, the plugin will use an OAuth client credential which is packaged with the plugin.
If you'd like to set up your own client credential, skip to the "Custom Client Credentials" section and then return here afterwards.
To authorize WordPress to fetch data from Google Drive, it will need an authorization code provided by Google.
From the settings page for the plugin, click "Get Authorization Code" under the "Authorize User" section, and you will be taken to a Google login page.
---> It is highly recommended to use a Google Generic Account <---
Select your user account if prompted, then accept the authorization prompt. Because we selected the drive.readonly scope before, it will only ask for readonly permissions.
Copy the code given into the box in the settings menu, and click "Authorize User Account"
The final step is to select a folder in Google Drive to sync the content to WordPress.
Navigate to the folder in Google Drive you wish to sync in your browser.
Note that the user account we just authorized in WordPress must have read access to this folder.
You cannot sync the top-level "My Drive" for an account, you must select a folder.
You can sync a Shared Drive, or any folder in a Shared Drive, as long as the authorized user has access to it.
Copy and paste the URL of the folder into the box in the settings menu, then click "Submit".
Once you have submitted your Folder URL, your setup is complete!
The settings menu will now show you some status information about the authorized user, the Google Drive folders you are syncing, and API usage statistics.
You can add or remove additional folders from here, if you have more content you'd like to sync.
Syncing Files into Media Library
Files will automatically sync from Google to the Media Library once an hour. You can also manually trigger a sync from the settings page.
Navigate to your media library, and you should see the synced files appear.
You can use these files just like any other file that was uploaded to WordPress! Embed them in a page, protect them with the Protected Content plugin, etc.
Server Rewrite Rules
In order to serve the contents of the files to browsers, the plugin uses a rewrite rule to capture requests and inject its own content.
These rewrite rules should be set up automatically in the .htaccess file when the plugin is activated, and removed when the plugin is deactivated.
They are included here in case you need to set up rules in the vhost config, or if the .htaccess modification fails for some reason.
The plugin should perform a self-check to determine if the rewrite rules are working correctly, and display an alert if they are not.
# BEGIN Google Drive Block
RewriteRule ^wp-content/uploads/(sites/[0-9]+/)?drive/(.*)$ index.php?gdpr_file=$2 [QSA,L]
# END Google Drive Block
Uninstalling the Plugin
From the settings menu, select "Purge Files from Media Library" to remove all synced files from the media library.
Revoke the authorized user access and forget the client credentials .
Then deactivate and remove the plugin.
Custom Client Credentials
Although the plugin ships with a bundled OAuth client credential file, you may wish to use your own client credentials.
While most sites should be fine using the bundled credentials, if your site has an exceptionally high rate of access, using a separate credential may be better.
Additionally, if you wish to monitor the API usage from the Google Console, you'll need to set up your own credentials since you won't have access using the bundled one.
To bypass the bundled credential file, you'll need to ad the following line to your wp-config.php file for WordPress:
define('NCSU_DRIVE_DEFAULT_CLIENT', false);
Next, set up and configure a new project and OAuth credential in the Google Cloud Console: https://console.cloud.google.com/apis/dashboard
--- > It is highly recommended to log in as a Google Generic Account for this process! < ---
Click on "Create Project". If you already have a an existing project, you can use the existing project, but it is recommended to create a new one.
To create a new project if you already have an existing project, click the dropdown where the project name is located in the upper left, then click "New Project"
When creating your project, you can name it whatever you like, but it is recommended to use a descriptive name related to your site.
Select "ncsu.edu" for the Organization, and select your department from the Location list. If you do not see your department in the list, select "Experimental Projects".
Click on "Create" to create your project.
After creating your project, you should be at the APIs and Services dashboard for your project.
If you do not see this page, click on the hamburger menu in the upper left corner, and select APIs and Services.
From the APIs and Services dashboard, click on "+ Enable APIs and Services".
Search for "Drive", and click on "Google Drive API" when it comes up.
At the top of the page, click "Enable API"
Now that the API is enabled for this project, we need to set up the OAuth consent page and create some credentials.
Click on "credentials" on the left side of the page.
Click "Configure Consent Screen"
Select "Internal" as the User Type, and click "Create"
Enter a name such as "WordPress Drive". Only the site admin connecting the WordPress site to Google will see this information.
Select your user account's email address for both the user support email address and developer contact information.
You can leave all the App Domain settings empty.
Click "Save and Continue"
Click on "Add or Remove Scopes", search for "drive.readonly", and check the box.
Click "Update" to return, then "Save and Continue" to arrive at the Summary page.
Now that we have configured the Consent Screen, we can create some credentials! Click back to "Credentials" on the left side of the page.
Click on "+ Create Credentials" at the top of the page, and select "OAuth Client ID"
Select "Web application" for the application type, and give it a name. As before, this name will only be seen by WordPress site administrators.
Click "Add URI" under the "Authorized redirect URIs" section, and add the following URL: https://wp-updates.oit.ncsu.edu/google-auth.php
Click "Create" and you should get a modal popup.
Click "Download JSON" and save the file to a safe place. We will need this file in a moment.
We're done with the Google side of things now, so feel free to close out of the Google Console.
Next, we'll be configuring the WordPress side.
After installing and activating the Google Drive Integration WordPress plugin, go to Settings -> Google Drive Settings.
If the default client credential was already loaded, click "Clear Client Secret" at the bottom of the page.
You will be prompted to upload a Client Secret file. Click "Choose File" and select the file you downloaded from the Google Console.
Click "Upload Client Secret" to proceed.
After the client secret is uploaded, setup proceeds as with using the bundled client credentials.