Customize and Style a Google Form on Your WordPress Site

Customize a Google Form for your WordPress site

Usually a contact form on WordPress is Contact Form 7 plus a plugin that saves the entries to the database, such as CFDB7. Sometimes a client prefers Google Forms, because the answers land in Google Sheets. Embedding the form as an <iframe> takes a minute, but the iframe content cannot be styled from your site.

Problem: A Google Form embedded on a WordPress page has to look like the rest of the site.

Solution: Build your own HTML form with the same field names as the Google Form and submit it to the Google Form URL. There are plugins for this, but the one I found had not been updated for about a year, so I did it in code.

Step 1. Create the form

Put the form on the page (in the editor or, for example, in an ACF WYSIWYG field). Use your own classes and IDs; the action and name attributes come in the next step.

<form id="google-form" class="info-form" action="#" method="post">
    <div class="form-group">
        <label for="email">Email Address *</label>
        <input id="email" class="form-control" required type="email" />
    </div>
    <div class="form-group">
        <label for="fn">First Name *</label>
        <input id="fn" class="form-control" required type="text" />
    </div>
    <div class="form-group">
        <label for="ln">Last Name *</label>
        <input id="ln" class="form-control" required type="text" />
    </div>
    <div class="form-group">
        <label for="check">What is 21 minus 3? * (making sure you are human)</label>
        <input id="check" class="form-control" required type="text" />
    </div>
    <span class="note">* Required</span>
    <div class="form-group">
        <input class="btn btn-orange" type="submit" value="Submit" />
    </div>
</form>

Step 2. Copy the action and field names from Google

Open the public link of the Google Form and look at the page source (or the browser inspector):

  • copy the action of the <form> tag, it ends with /formResponse;
  • copy the name of every field. They start with entry., for example name="entry.273330035". The exception is the email field when the form collects email addresses: it is called emailAddress.
<form id="google-form" class="info-form" action="https://docs.google.com/forms/d/e/YOUR_FORM_ID/formResponse" method="post">
    <div class="form-group">
        <label for="email">Email Address *</label>
        <input id="email" class="form-control" name="emailAddress" required type="email" />
    </div>
    <div class="form-group">
        <label for="fn">First Name *</label>
        <input id="fn" class="form-control" name="entry.273330035" required type="text" />
    </div>
    <div class="form-group">
        <label for="ln">Last Name *</label>
        <input id="ln" class="form-control" name="entry.837780272" required type="text" />
    </div>
    <div class="form-group">
        <label for="check">What is 21 minus 3? * (making sure you are human)</label>
        <input id="check" class="form-control" required type="text" />
    </div>
    <span class="note">* Required</span>
    <div class="form-group">
        <input class="btn btn-orange" type="submit" value="Submit" />
    </div>
</form>

The check field has no name: it is only for our own spam check and is not sent to Google.

At this point the form already works, but there are two problems:

  1. Spam. Google Forms does not support reCAPTCHA, so the “21 minus 3” question has to be checked by us.
  2. After submitting, the visitor lands on the Google “Your response has been recorded” page. It is better to stay on the site and show a message, or redirect to your own “thank you” page.

Step 3. Submit with JavaScript

Google does not send CORS headers for formResponse, so a normal AJAX request from your domain always ends in the error callback, even when the answer has been saved. The request has to be sent with fetch() in no-cors mode: the browser sends it, but the response cannot be read, so we show the success message once the request has gone out.

jQuery( function ( $ ) {
    $( '#google-form' ).on( 'submit', function ( e ) {
        e.preventDefault();
        var form = this;

        // Clean previous messages
        $( form ).find( '.success, .error' ).remove();

        // Our spam check
        if ( $( '#check' ).val().trim() !== '18' ) {
            $( '#check' ).after( '<span class="error">This value is incorrect. Please check it and try again.</span>' );
            return;
        }

        fetch( form.action, { method: 'POST', mode: 'no-cors', body: new FormData( form ) } )
            .then( function () {
                // window.location.href = '/thank-you/'; // uncomment to redirect instead
                $( '<p class="success">Submission successful</p>' ).appendTo( form );
                form.reset();
            } )
            .catch( function () {
                $( '<p class="error">Submission failed. Please try again or contact the administrator.</p>' ).appendTo( form );
            } );
    } );
} );

What remains is to style the form and the messages.

Bonus: the form in an ACF WYSIWYG field

If the form HTML is stored in a WYSIWYG field (or in the post content), WordPress adds <br> tags between the lines. Replace them with line breaks before output:

<?php
$form = preg_replace( '/<br\W*?\/?>/', "\n", get_field( 'form' ) ); // ACF WYSIWYG field
$form = preg_replace( '/<br\W*?\/?>/', "\n", get_the_content() );   // default editor
echo $form;

Summary

A Google Form on WordPress without a plugin is simple and fast, but keep in mind two drawbacks:

  1. If you add or change a field in the Google Form, you have to update the HTML on the site by hand, and the entry.* names may change.
  2. Field validation is only what you add yourself: HTML attributes like required and type="email", or your own JavaScript.

Sources:

  1. https://blog.webjeda.com/google-form-customize
  2. https://codepen.io/learningcode/post/customize-a-google-form-for-your-website