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].
add_split_test_forms
function?{
andendif;
, FYI. You need to either use theif (...) : ... endif;
syntax, or theif ( ... ) { ... }
syntax.