Add a Password to Any Gravity Form Without Conditional Logic
Imagine you’ve just wrapped up a sprint, your cup of coffee is running on fumes, and Slack suddenly pings:
“Hey–can you password-protect our 40-question survey?”
I got a message just like this from a client recently. They were needing me to password protect a massive application form on their website. That application wasn’t just any form–it was built with Gravity Forms, the Swiss Army knife of WordPress form builders. If you haven’t met it yet, Gravity Forms gives you drag-and-drop form creation, email notifications, payment integrations, and advanced conditional logic–all without wrestling with plugin bloat. Paired with the power of WordPress and rock-solid hosting from Pressable, it’s the backbone of almost all my client projects.
From Conditional Logic Fake Out to Genuine Password Protection
In the past, when asked to “password-protect” a Gravity Form, I reached for what I now call the toothpick trick:
- Add a single text field at the top of the Gravity Form form: “Enter password here.”
- For each of the fields or sections, set up a conditional rule: “Show this field if password = yourSecret.”
- Hit Preview, cross your fingers, and hope you didn’t make a typo.
It sort of works–but the moment you change the password, you have to hunt down and update dozens of hidden-field rules. Worse, users see fields flicker on as they type and it does not feel like actual password protection (because it isn’t). It felt less like a secure gate and more like a haunted house: one wrong keystroke on the backend when updating conditional logic rules, and half the form disappears.
One late night (around cup #4 of coffee) as I started implementing my toothpick trick, I realized: why not build a single gate–one password prompt that stands guard at the door–rather than slapping conditional logic everywhere? The result is a tiny, self-contained PHP snippet that transforms any Gravity Form into a fortress door.
How the Snippet Works and Performs
Rather than scattering logic across dozens of fields, the snippet:
- Adds a toggle under Form → Settings → Restrictions: just check “Require a password to view this form.”
- Displays a neat prompt instead of the form until the correct password is entered.
- Unlocks the form with hardened, server-side tokens—no password ever lives in the browser.
- Smoothly scrolls the prompt into view on error, and scrolls to the form on success.
All without a single conditional-logic rule in your form itself. My toothpick trick became obsolete with this snippet and I saved myself time!
Why You’ll Love It
- Zero Form-Editing Gymnastics No more tedious setup for every question. One click in settings, one password, and you’re done.
- Native Theme Styling The prompt box, input field, and submit button automatically inherit your active theme’s fonts, colors, and focus styles—no extra CSS.
- Built-In UX Magic
- On wrong password, the prompt scrolls into view (goodbye, missed gates).
- On correct password, the real form scrolls up for an immediate start.
- Transparent Security
- Server-Side Tokens: We generate random tokens with wp_generate_password() and store them in WordPress transients (24 hr default).
- CSRF Protection: Each prompt includes a wp_nonce_field(), verified on submit.
- Hardened Cookies: Flags secure, httponly, and samesite=Lax lock down your tokens.
- Instant Invalidation Change the password in settings and all old tokens vanish—no stale keys left behind.
- Zero Asset Overhead All styling is inlined via wp_add_inline_style(). One snippet, no extra files or enqueued stylesheets.
Security Deep-Dive (For the Nerds)
When someone enters the correct password:
- A 20-character token is generated and stored in a transient on your server.
- A hardened cookie holds that token—your password itself never sees the browser.
- On each page load, we check get_transient() + the cookie before rendering the real form.
- If you ever change the password, a save-hook wipes all related transients immediately.
How to Get Started
- Copy the Code: Grab the snippet and paste it into your theme’s functions.php file, or add it via your favorite code snippets plugin.
- Configure your form: open Gravity Forms → Settings → Restrictions, check Require a password to view this form, and type your secret.
- Embed & Enjoy: place your form via shortcode or block—visitors first meet the steel gate, then step right into your secure form once unlocked.
That coffee-fueled epiphany turned dozens of toothpick rules into a single, elegant gate. If you want to lock down your Gravity Forms without the headache, give this snippet a spin—and while you’re at it, grab your copy of Gravity Forms, host your site on Pressable, and revel in the simplicity.
If you need any help implementing this on your site or have questions about customization, drop us a message—we’re here to help!