Authenticate WordPress Users with Google Apps Script

A
Published in: Google Apps Script - WordPress

You can use the XMLRPC library with Google Scripts to authenticate users that are members of your WordPress or Blogger website.

For instance, if you have a Google Form that you only wish to serve to registered members of your WordPress or BuddyPress website, you can add a layer of authentication before serving the form using the HTML service. This layer will connect to the website through XML RPC to verify if the connecting user is valid or not.

The Google Script can be further customized to only allow users with certain roles -like allow authors and admin but not subscribers. This can be used in conjunction with Easy Digital Downloads to serve files to only users that have the WordPress role as EDD_Subscriber or similar.

function verifyWordPressUser(username, password) {

  // Library key: My_8O8KRa_MszCVjoC01DTlqpU7Swg-M5

  /* Replace example.com with your WordPress blog URL */
  var wordpressURL = "http://example.com/xmlrpc.php";

  /* Call the wp.getUsers API method to get User details */
  var request = new XMLRPC.XmlRpcRequest(wordpressURL, 'wp.getUsers');

  /* The first parameter is empty since there's no blog ID for WordPress */
  request.addParam("");
  request.addParam(username);
  request.addParam(password);

  var response = request.send().parseXML();

  if (response.faultCode) {
    throw(response.faultString);
  }
  else {
    throw(response[0].display_name  + " is a valid user");
  }

}
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - WordPress

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube