{"id":10358,"date":"2025-08-29T15:32:04","date_gmt":"2025-08-29T12:32:04","guid":{"rendered":"https:\/\/aitooldetective.com\/?p=10177"},"modified":"2025-08-29T16:08:24","modified_gmt":"2025-08-29T13:08:24","slug":"how-to-prevent-links-in-gravity-forms-fields-with-a-simple-snippet","status":"publish","type":"post","link":"https:\/\/fspirits.com\/fr\/how-to-prevent-links-in-gravity-forms-fields-with-a-simple-snippet\/","title":{"rendered":"How to Easily Block Links in Gravity Forms: Step-by-Step Guide"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/gravity-forms\"   >Gravity Forms<\/a> is one of the most powerful form builders for WordPress.<br>It\u2019s flexible, feature-rich, and integrates seamlessly with countless <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/envato-wp-unlimited-thems\"   >plugins<\/a>.<br>But like any form system, it can be misused.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One common issue WordPress site owners face is <strong>users submitting unwanted links or URLs<\/strong> inside form fields. These links can clutter your database, introduce spam, and in some cases even pose a <strong>security risk<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Fortunately, with just one small snippet of code, you can completely <strong>block links from being submitted into your <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/gravity-forms\"   >Gravity Forms<\/a> fields<\/strong>\u2014while still allowing users to enter their email addresses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this guide, you\u2019ll learn step by step how to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Install and use the <strong>Code Snippets<\/strong> <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/envato-wp-unlimited-thems\"   >plugin<\/a>.<\/li>\n\n\n\n<li>Add a custom function that prevents URLs from being submitted.<\/li>\n\n\n\n<li>Apply the validation to a specific form and exclude the email field.<\/li>\n\n\n\n<li>Keep your forms clean, safe, and professional.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Block Links in <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/gravity-forms\"   >Gravity Forms<\/a>?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">There are several reasons you may want to stop links from being submitted into your <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/gravity-forms\"   >Gravity Forms<\/a>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Spam Prevention<\/strong> \u2013 Bots and spammers often drop links in forms hoping to promote their websites.<\/li>\n\n\n\n<li><strong>Clean Data Collection<\/strong> \u2013 If you\u2019re collecting feedback, surveys, or customer input, you don\u2019t want messy URLs cluttering up your entries.<\/li>\n\n\n\n<li><strong>Security Hardening<\/strong> \u2013 Some links can redirect to malicious sites, creating unnecessary risk for your team or clients reviewing submissions.<\/li>\n\n\n\n<li><strong>Professionalism<\/strong> \u2013 Preventing links ensures only relevant content is entered, keeping your data consistent and trustworthy.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">The Tool You\u2019ll Need: Code Snippets <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/envato-wp-unlimited-thems\"   >Plugin<\/a><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of editing your <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/envato-wp-unlimited-thems\"   >theme<\/a>\u2019s <code>functions.php<\/code> file (which is risky and gets reset when you update your <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/envato-wp-unlimited-thems\"   >theme<\/a>), you can use the <strong>Code Snippets<\/strong> <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/envato-wp-unlimited-thems\"   >plugin<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Snippets<\/strong> is a free <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/envato-wp-unlimited-thems\"   >plugin<\/a> that lets you safely add custom PHP code to your WordPress site without touching core files. It provides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A clean interface for adding snippets.<\/li>\n\n\n\n<li>The ability to enable\/disable snippets without losing them.<\/li>\n\n\n\n<li>No need for a child <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/envato-wp-unlimited-thems\"   >theme<\/a>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udc49 To install it:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to <strong><a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/envato-wp-unlimited-thems\"   >Plugins<\/a> \u2192 Add New<\/strong> in your WordPress dashboard.<\/li>\n\n\n\n<li>Search for <strong>Code Snippets<\/strong>.<\/li>\n\n\n\n<li>Install and activate it.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Now you\u2019re ready to add your custom code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Snippet Code to Block Links in <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/gravity-forms\"   >Gravity Forms<\/a><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s the exact snippet you\u2019ll need. It works on <strong>Form ID 9<\/strong>, and blocks links in every field <strong>except the Email field (ID 2)<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Block URLs in ANY field of form ID 9 except the Email field (ID 2)\nadd_filter( 'gform_field_validation_9', 'block_urls_except_email', 10, 4 );\nfunction block_urls_except_email( $result, $value, $form, $field ) {\n\n    \/\/ Skip validation for Email field\n    if ( $field-&gt;id == 2 ) {\n        return $result;\n    }\n\n    $nourl_pattern = '\/https?:\/\/|www.|&#091;a-z0-9-]+.&#091;a-z]{2,}\/i';\n\n    \/\/ Recursive function to check arrays for URLs\n    $contains_url = function($val) use ($nourl_pattern, &amp;$contains_url) {\n        if ( is_array($val) ) {\n            foreach ($val as $v) {\n                if ($contains_url($v)) return true;\n            }\n            return false;\n        }\n        return is_string($val) &amp;&amp; preg_match($nourl_pattern, $val);\n    };\n\n    if ( $contains_url($value) ) {\n        $result&#091;'is_valid'] = false;\n        $result&#091;'message']  = 'Message cannot contain website addresses.';\n    }\n\n    return $result;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">How This Code Works<br><br>Let\u2019s break it down:<br><br>add_filter( &#8216;gform_field_validation_9&#8217;, &#8230; )<br>This tells <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/gravity-forms\"   >Gravity Forms<\/a> to run our custom validation function on form ID 9.<br><br>if ( $field-&gt;id == 2 )<br>This ensures the Email field (ID 2) is excluded, so users can still submit email addresses.<br><br>$nourl_pattern<br>This regular expression (regex) checks for any pattern that looks like a URL (http, https, www, or domain extensions like .com, .org, etc.).<br><br>$contains_url<br>A recursive function that checks arrays or strings to detect any URLs.<br><br>$result[&#8216;is_valid&#8217;] = false;<br>If a link is detected, the field will fail validation, and the user will see the error message.<br><br>$result[&#8216;message&#8217;] = &#8216;Message cannot contain website addresses.&#8217;;<br>This is the error message displayed when someone tries to submit a link.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step-by-Step: Adding the Snippet<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to <strong>Snippets \u2192 Add New<\/strong> in your WordPress dashboard.<\/li>\n\n\n\n<li>Give your snippet a descriptive name, like: <em>\u201cBlock URLs in Gravity Form 9\u201d<\/em>.<\/li>\n\n\n\n<li>Paste the code into the editor.<\/li>\n\n\n\n<li>Choose <strong>Only run on site front-end<\/strong> (important for form submissions).<\/li>\n\n\n\n<li>Save and activate the snippet.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it! \ud83c\udf89 Your form is now protected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing Your Gravity Form<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Try submitting your form with a normal text response \u2192 \u2705 It should work.<\/li>\n\n\n\n<li>Try adding a URL like <code>http:\/\/example.com<\/code> \u2192 \u274c It should trigger the validation error.<\/li>\n\n\n\n<li>Try entering an email address in the email field \u2192 \u2705 It should still allow it.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This ensures your form only accepts clean, useful input.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Final Thoughts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Blocking links in <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/gravity-forms\"   >Gravity Forms<\/a> fields is a smart way to <strong>protect your website from spam, keep your entries clean, and improve security<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using the <strong>Code Snippets <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/envato-wp-unlimited-thems\"   >plugin<\/a><\/strong> makes it easy to manage this code without editing your <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/envato-wp-unlimited-thems\"   >theme<\/a> files. And the snippet provided here is flexible\u2014you can modify the <strong>Form ID<\/strong> or <strong>excluded field IDs<\/strong> to fit your needs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udc49 Now your <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/gravity-forms\"   >Gravity Forms<\/a> are safer, smarter, and more professional.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you found this guide helpful, don\u2019t forget to <strong>Like, Share, and Subscribe<\/strong> to our content for more <strong>WordPress tutorials, tips, and snippets<\/strong>. \ud83d\ude80<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-width wp-block-button__width-75\"><a class=\"wp-block-button__link has-white-color has-vivid-red-background-color has-text-color has-background has-link-color wp-element-button\" href=\"https:\/\/aitooldetective.com\/go\/gravity-forms\" target=\"_blank\" rel=\"noreferrer noopener\">Find Out More About Gravity Forms<\/a><\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Secure Gravity Forms from Links with One Snippet\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/vqDYyFTPqrw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to block unwanted links from being submitted in <a  class=\"btl_autolink_hyperlink\"  href=\"https:\/\/fspirits.com\/go\/gravity-forms\/\"   >Gravity Forms<\/a> and keep your data clean and secure with just a small snippet of code.<\/p>","protected":false},"author":1,"featured_media":10360,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[2355,580],"tags":[2559],"class_list":["post-10358","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-latest-news","category-wordpress","tag-gravity-forms"],"_links":{"self":[{"href":"https:\/\/fspirits.com\/fr\/wp-json\/wp\/v2\/posts\/10358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fspirits.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fspirits.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fspirits.com\/fr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fspirits.com\/fr\/wp-json\/wp\/v2\/comments?post=10358"}],"version-history":[{"count":0,"href":"https:\/\/fspirits.com\/fr\/wp-json\/wp\/v2\/posts\/10358\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fspirits.com\/fr\/wp-json\/wp\/v2\/media\/10360"}],"wp:attachment":[{"href":"https:\/\/fspirits.com\/fr\/wp-json\/wp\/v2\/media?parent=10358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fspirits.com\/fr\/wp-json\/wp\/v2\/categories?post=10358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fspirits.com\/fr\/wp-json\/wp\/v2\/tags?post=10358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}