0

Please bear with me as I am a front-end developer trying to grapple with PHP.

I am attempting to add a gravity form to specific pages on my website website by adding a function to my functions.php file. It doesn't matter where it is placed on the page since the form will be initially hidden and then used for an exit-intent popup. Gravity form's documentation says that a Gravity form can be embedded via PHP function call. This is their given example:

gravity_form( 1, false, false, false, '', false );

After searching through already asked questions I found this post about conditional PHP that would only call the function on the specified page. I tried to put this all together. The result is below:

function add_split_test_forms()
{
    if (is_page('Homepage')):
    gravity_form(19, false, false, false, '', false);
    endif;
}

I added "add_split_test_forms" because I assume this is just me naming the function (correct me if I'm wrong there).

Unfortunately, this is not working. I tried other variations such as using the page id instead of the page name, but the result is the same.

I went back to the post regarding conditional PHP and tried the other upvoted answer (the accepted answer) which resulted in me trying this code:

function add_split_test_forms()
{
  global $post;

  <?php if( $post->ID == 5457) { ?>
      gravity_form(19, false, false, false, '', false);
      endif;
  <?php } ?>
}

This just broke everything so I also tried:

function add_split_test_forms()
{
  global $post;

  if( $post->ID == 5457) { 
      gravity_form(19, false, false, false, '', false);
      endif;
  }
}

This also broke everything. So... now I'm back to the first example that was, at a minimum, not bringing the entire site down.

At the end of that documentation page it says:

When embedding a form via a function call you must also manually include the necessary Gravity Forms related Javascript and CSS via the built in WordPress enqueue capabilities. Gravity Forms does not include these by default when calling a form via a function call and they are necessary for forms that contain conditional logic or the date picker field.

We strongly recommend you enqueue the scripts rather than including them as hardcoded calls in your theme. Implementing it this way will insure that Gravity Forms does not include them on the page if they are already present. It is also a good practice to only load these scripts on the front end.

Gravity Forms 1.5 introduced the gravity_form_enqueue_scripts() function which allows you to easily enqueue the necessary Gravity Forms’ scripts and styles when manually embedding a form. This is also useful if you are using a GF widget and do not wish for the styles and scripts to be loaded inline.

When I looked at the documentation for gravity_form_enqueue_scripts it provided this example gravity_form_enqueue_scripts( 4, true ); and said:

This script should be placed in the theme’s header.php file just before the wp_head() function is called.

But can't I just add it to the functions.php file? When I tried:

function add_split_test_forms()
{
    if (is_page('Homepage')):
        gravity_form_enqueue_scripts( 19, true );
        gravity_form(19, false, false, false, '', false);
    endif;
}

AND

gravity_form_enqueue_scripts( 19, true );

function add_split_test_forms()
{
    if (is_page('Homepage')):
gravity_form(19, false, false, false, '', false);
    endif;
}

Neither way works. Do I need to hook into the wp_head() somehow in my functions.php file to add gravity_form_enqueue_scripts( 19, true );?

Important notes to preempt what questions I can think of:

  • This being added to the end of my functions.php file before the closing ?>.
  • Yes this form exists and functions correctly on other pages.
  • I've looked at the the other posts here regarding page-conditional PHP functions and none of them address this issue.
  • Per the Gravity Forms documentation, I'm not even getting the "Oops! We could not locate your form." error. There is absolutely zero output (as far as I can tell). Not visible changes to the page either.
  • I also looked through the posts under the little-used tag of [page-specific-settings].
  • 2
    Where do you call the add_split_test_forms function? – Milo Nov 19 '18 at 15:54
  • 1
    why not put a gravity forms shortcode in your homepage content and be done? – majick Nov 19 '18 at 16:15
  • The two code snippets that "broke everything" are due to mixing up the { and endif;, FYI. You need to either use the if (...) : ... endif; syntax, or the if ( ... ) { ... } syntax. – Pat J Nov 19 '18 at 17:04
0

if you got following in your functions.php

function add_split_test_forms()
{
    if (is_page('Homepage')):
    gravity_form(19, false, false, false, '', false);
    endif;
}

you will at least need to run this function once: add a line add_split_test_forms() in your functions.php.

if your condition if (is_page('Homepage')): is correct. (I prefer if ( is_front_page()): should also work.) Then your form should appear in the markup, like you said.

  • So, after the function I can just add add_split_test_forms on a new line? How do I run a function in PHP? Or add_action('add_split_test_forms');? – Telarian Nov 19 '18 at 16:09
  • @Orion PHP has some good pages in its documentation, one is about functions – kero Nov 19 '18 at 16:11
0

I'd recommend that you use the the_content filter hook to add your form:

add_filter( 'the_content', 'add_split_test_forms' );

function add_split_test_forms( $content ) {
    if ( is_page( 'Homepage' ) ) {
        $content .= gravity_form( 19, false, false, false, '', false, 1, true );
        return $content;
    }
}

... and the wp_enqueue_scripts action hook to enqueue the scripts:

add_action( 'wp_enqueue_scripts', 'wpse319642_add_scripts_for_form_19' );
function wpse319642_add_scripts_for_form_19() {
    // 2nd param is 'false' b/c you've set AJAX to 'false' in the gravity_form() call
    gravity_form_enqueue_scripts( 19, false );
}

All this code can go in your theme's functions.php file.

This code is untested, but I think it should work.

(Echoing @majick's comment, though: Is there a compelling reason to not simply use the Gravity Forms shortcode to embed the form in the WordPress editor?)

  • I'll give this a try, but the reason for not adding via shortcode in the editor is because I need to add this form to a lot of pages. It is not feasible to do it page by page. – Telarian Nov 19 '18 at 16:48
  • So when I tried this I see new <link>'s added like <link rel="stylesheet" id="gforms_browsers_css-css" href="path/to/my/css/browsers.min.css" type="text/css" media="all"> but the form isn't actually present on the page. – Telarian Nov 19 '18 at 16:54
  • Did you add both code snippets? The first one's intended to display the form, and the second to enqueue the necessary scripts / CSS. – Pat J Nov 19 '18 at 17:32
  • The reason why I don't want to embed it in the editor is because I may need to display this form on 20-30 pages at time. And we're performing split testing so I'll be trying different sets of pages. It will take much longer to add the form to each page individually. I don't really have time for that lol. – Telarian Dec 13 '18 at 19:40
-1

Ok I figured it out. I just need to hook into wp_footer. The only thing that gets added to header.php is the gravity_form_enqueue_scripts() function. Per the documentation:

This function will enqueue the necessary styles and scripts for the specified Gravity Form. This is useful when manually embedding a form outside the WordPress loop using the function call or to force the stylesheets and scripts to load in the header when using the Form Widget.

function add_split_test_forms()
{
    if (is_page([4904,2559,356,358,360,376,362,364,366,354,372,374])):
        gravity_form(19, false, false, false, '', false);
    endif;
}
add_action( 'wp_footer', 'add_split_test_forms' );

Edit: Jacob suggested below that forms should never go in the <head> but rather in the footer. I've changed my answer to reflect that.

  • 1
    A form cannot go inside the <head> element, so wp_head is definitely not the right hook here. You'd be better off using wp_footer. – Jacob Peattie Nov 20 '18 at 0:36
  • The Gravity Forms documentation says "This script should be placed in the theme’s header.php file just before the wp_head() function is called." docs.gravityforms.com/gravity_form_enqueue_scripts/#placement – Telarian Nov 20 '18 at 15:46
  • @JacobPeattie BUT I tried adding it to the footer instead and that works as well, I'll update my answer. – Telarian Nov 20 '18 at 15:48
  • WAIT. I'm wrong. gravity_form_enqueue_scripts( 4, true ); is supposed to go in the head, not the form call itself. I misread it. – Telarian Nov 20 '18 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.