How to Block Links in Gravity Forms: Step-by-Step Guide

Gravity Forms is one of the most powerful form builders for WordPress.
It’s flexible, feature-rich, and integrates seamlessly with countless plugins.
But like any form system, it can be misused.

One common issue WordPress site owners face is users submitting unwanted links or URLs inside form fields. These links can clutter your database, introduce spam, and in some cases even pose a security risk.

Fortunately, with just one small snippet of code, you can completely block links from being submitted into your Gravity Forms fields—while still allowing users to enter their email addresses.

In this guide, you’ll learn step by step how to:

  • Install and use the Code Snippets plugin.
  • Add a custom function that prevents URLs from being submitted.
  • Apply the validation to a specific form and exclude the email field.
  • Keep your forms clean, safe, and professional.

Why Block Links in Gravity Forms?

There are several reasons you may want to stop links from being submitted into your Gravity Forms:

  1. Spam Prevention – Bots and spammers often drop links in forms hoping to promote their websites.
  2. Clean Data Collection – If you’re collecting feedback, surveys, or customer input, you don’t want messy URLs cluttering up your entries.
  3. Security Hardening – Some links can redirect to malicious sites, creating unnecessary risk for your team or clients reviewing submissions.
  4. Professionalism – Preventing links ensures only relevant content is entered, keeping your data consistent and trustworthy.

The Tool You’ll Need: Code Snippets Plugin

Instead of editing your theme’s functions.php file (which is risky and gets reset when you update your theme), you can use the Code Snippets plugin.

Code Snippets is a free plugin that lets you safely add custom PHP code to your WordPress site without touching core files. It provides:

  • A clean interface for adding snippets.
  • The ability to enable/disable snippets without losing them.
  • No need for a child theme.

👉 To install it:

  1. Go to Plugins → Add New in your WordPress dashboard.
  2. Search for Code Snippets.
  3. Install and activate it.

Now you’re ready to add your custom code.

The Snippet Code to Block Links in Gravity Forms

Here’s the exact snippet you’ll need. It works on Form ID 9, and blocks links in every field except the Email field (ID 2).

// Block URLs in ANY field of form ID 9 except the Email field (ID 2)
add_filter( 'gform_field_validation_9', 'block_urls_except_email', 10, 4 );
function block_urls_except_email( $result, $value, $form, $field ) {

    // Skip validation for Email field
    if ( $field->id == 2 ) {
        return $result;
    }

    $nourl_pattern = '/https?://|www.|[a-z0-9-]+.[a-z]{2,}/i';

    // Recursive function to check arrays for URLs
    $contains_url = function($val) use ($nourl_pattern, &$contains_url) {
        if ( is_array($val) ) {
            foreach ($val as $v) {
                if ($contains_url($v)) return true;
            }
            return false;
        }
        return is_string($val) && preg_match($nourl_pattern, $val);
    };

    if ( $contains_url($value) ) {
        $result['is_valid'] = false;
        $result['message']  = 'Message cannot contain website addresses.';
    }

    return $result;
}

How This Code Works

Let’s break it down:

add_filter( ‘gform_field_validation_9’, … )
This tells Gravity Forms to run our custom validation function on form ID 9.

if ( $field->id == 2 )
This ensures the Email field (ID 2) is excluded, so users can still submit email addresses.

$nourl_pattern
This regular expression (regex) checks for any pattern that looks like a URL (http, https, www, or domain extensions like .com, .org, etc.).

$contains_url
A recursive function that checks arrays or strings to detect any URLs.

$result[‘is_valid’] = false;
If a link is detected, the field will fail validation, and the user will see the error message.

$result[‘message’] = ‘Message cannot contain website addresses.’;
This is the error message displayed when someone tries to submit a link.

Step-by-Step: Adding the Snippet

  1. Go to Snippets → Add New in your WordPress dashboard.
  2. Give your snippet a descriptive name, like: “Block URLs in Gravity Form 9”.
  3. Paste the code into the editor.
  4. Choose Only run on site front-end (important for form submissions).
  5. Save and activate the snippet.

That’s it! 🎉 Your form is now protected.

Testing Your Gravity Form

  • Try submitting your form with a normal text response → ✅ It should work.
  • Try adding a URL like http://example.com → ❌ It should trigger the validation error.
  • Try entering an email address in the email field → ✅ It should still allow it.

This ensures your form only accepts clean, useful input.

Final Thoughts

Blocking links in Gravity Forms fields is a smart way to protect your website from spam, keep your entries clean, and improve security.

Using the Code Snippets plugin makes it easy to manage this code without editing your theme files. And the snippet provided here is flexible—you can modify the Form ID or excluded field IDs to fit your needs.

👉 Now your Gravity Forms are safer, smarter, and more professional.

If you found this guide helpful, don’t forget to Like, Share, and Subscribe to our content for more WordPress tutorials, tips, and snippets. 🚀

Check Out Amazing, Time Saving Marketing Tools

Picture of Constantinos Albanidis

Constantinos Albanidis

As a digital nomad of 10 years now, I'm an expert full-stack marketer who loves helping businesses expand their online presence. With my background in web design, I craft clean, convertible websites that help clients attract and engage customers.Outside of client work, I enjoy researching and testing the latest AI tools. As an early adopter of conversational technologies, I love sharing how bots can enhance marketing efforts. I also publish a popular blog discussing ethical use cases for AI in business.When not coding or collaborating with AI, I pursue my passion for using strategic content and social media to grow brands. With a specialty in automated traffic generation, I help companies drive qualified leads through organic sharing.I strive to stay on top of evolving digital trends so I can advise clients on the most effective tactics. Community is also core to my values, so you'll often find me volunteering my skills for nonprofit causes. Reach out to discuss your goals - I'm always eager to help others succeed online.

Follow Our Page

Recent Posts

A.I. Tools

Create Studio Pro Review: Features and Pricing Explained

<a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fhotdeal4all.com%2Fcreate-studio-pro-review-features-and-pricing-explained%2F&linkname=Create%20Studio%20Pro%20Review%3A%20Features%20and%20Pricing%20Explained" title="Facebook" rel="nofollow
Read More »
Crypto

Crypto Prices Plunge: What You Need to Know

<a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fhotdeal4all.com%2Fcrypto-prices-plunge-what-you-need-to-know%2F&linkname=Crypto%20Prices%20Plunge%3A%20What%20You%20Need%20to%20Know" title="Facebook" rel="nofollow
Read More »

Sign up for our Newsletter

Subscribe to my exclusive newsletter and gain access to a treasure trove of insider tips, expert strategies, and cutting-edge insights

Check Out Amazing, Time Saving Marketing Tools

A.I. Prompts

Uncovering Designrr: A Comprehensive Review

In this post, we explore Designrr, a robust content creation tool that simplifies the ebook writing process for writers, podcasters, and small business owners alike. With its innovative Wordgenie engine, Designrr allows users to effortlessly generate content or convert existing materials—such as blog posts and podcasts—into professionally crafted ebooks in mere minutes. Compatible with both Windows and Mac, this platform offers a range of features including importing content from various sources, easy editing and customization, and a selection of stunning templates. Users can export their work in multiple formats like PDF, Kindle, and ePub, making it a versatile solution for any content creator. Join us as we delve into Designrr’s standout features, user experiences, and an honest assessment of its pros and cons. Whether you’re looking to enhance your content creation workflow or seeking a reliable tool for your next project, this comprehensive review will provide the insights you need.

Read More »
A.I. Tools

FlexClip Black Friday Sale: Upgrade Your Video Editing

Discover the perfect opportunity for creators this Black Friday with FlexClip‘s Mega Sale, offering unprecedented discounts on video editing plans. Enjoy 40% off annual subscriptions using code FCBFAS and 30% off monthly plans and AI credit packs with code FCBFMC, valid until December 5, 2025. If you’ve ever struggled with complex video editing software, FlexClip provides a refreshing alternative, making the editing process intuitive and enjoyable. With its user-friendly interface, you can focus on creating engaging content without the hassle of overwhelming features. This sale is an ideal chance to enhance your content creation workflow, whether you’re a seasoned pro or just starting out. Don’t miss this opportunity to elevate your video projects and boost your engagement with FlexClip‘s powerful editing tools!

Read More »
A.I. Prompts

Elementor 3.33: Key Features and Benefits for Web Creators

Elementor 3.33 marks a significant update in the world of website building, enhancing the experience for both beginners and seasoned web creators. This latest version is built on the new Editor V4, introducing a more intuitive and scalable design workflow. Key features include the Variables Manager, element-level Custom CSS, and improved media handling, all designed to push the limits of no-code web development. The update prioritizes a CSS-first approach, ensuring faster style loading and cleaner page rendering, while making complex design systems easier to manage. Whether you’re creating your first landing page or overseeing extensive multi-site projects, Elementor 3.33 offers the tools necessary to streamline your web design process. Discover how these enhancements not only elevate your workflow but also contribute to a more efficient and effective web-building experience.

Read More »
Hot Deal
Logo
Register New Account

Join our Newsletter:
Stay Ahead of the Game!!!

Join our exclusive newsletter community today and unlock a world of valuable insights, expert advice, and exciting updates delivered right to your inbox, empowering you to stay ahead and make the most of every opportunity.

Subscription Form
Shopping cart