has_gform() – Gravity Forms Conditional Statement

I needed a function to test whether or not a Gravity Form was present on any given page of a WordPress site I’m working on. There didn’t seem to be anything like that out on the Internet, and contacting support they weren’t aware of anything built in, so I figured I’d give it the ol’ Queen’s best and put something together myself.

For your functions.php file:

// conditional to check whether Gravity Forms shortcode is on a page
function has_gform() {
global $post;
$all_content = get_the_content();
if (strpos($all_content,'[gravityform') !== false) {
return true;
} else {
return false;
}
}

Note that this will only check whether or not there is a Gravity From shortcode within the post’s content. If you manually embed or use a widget, this won’t cover you. It works for my needs though.

What the Code Does

First we call the global variable $post, which we need to get information about the page/post/custom post type in our function. That’s because our next line of code, which includes get_the_content(), only works in the loop. The global call to $post allows us to get around that.

So we set all of the content into the $all_content variable, and then check to see if [gravityform (the beginning of every Gravity Form shortcode) exists within that variable.

Practical Example

I did this because I like to register my scripts, including jQuery, in the footer of my sites. Google likes this for Pagespeed, and it apparently allows your site to load a bit more quickly.

However, Gravity Forms inserts jQuery directly into your pages wherever a form exists, not simply at the bottom of the file. So any calls to jQuery that come before jQuery itself has loaded will not work.

So what I do is say “if the page has a Gravity Form shortcode, output jQuery in the head section, otherwise put it at the bottom of the page.”

That code looks like this (including deregistering jQuery that comes packaged with WP and using Google’s CDN version):

function click_scripts() {
wp_deregister_script( 'jquery' );
if (!has_gform()) {
wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', '', null, true);
} else {
wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', '', null, false);
}
wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'click_scripts');

If you’re not familiar with wp_register_script, you’ll want to look into that over on the Codex, but basically the two highlighted bits there “true” and “false”, tell WordPress to print the scripts at the bottom of the page (true) or leave them at the top as is default (false).

What About Manually Including Gravity Forms via the PHP Function?

It’s possible to add a form to your page without using the shortcode, such as in a theme file. If you wanted to have these pages included in your conditional check, you could manually add them to the function, like so:

// conditional to check whether Gravity Forms shortcode is on a page, or if it's a particular page where you've added the form manually
function has_gform() {
global $post;
$all_content = get_the_content();
if (strpos($all_content,'[gravityform') !== false || is_page('99') {
return true;
} else {
return false;
}
}

Replace “99” with your Page ID, or an array of Page IDs.

Up Next: Getting Started with Stripe Payment Processing