PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Enqueuing scripts for custom content area

  1. I'm using some custom meta boxes with TinyMCE fields to let the client edit separate tabs on a page. When they embed a Gravity Form in one of these fields, the necessary scripts don't get enqueued.

    It looks like this is because GFFormDisplay::enqueue_scripts looks through the current WP query content for forms. I'm wondering, might it be possible for Gravity Forms to scan for forms using the the_content filter? I output all my custom TinyMCE fields with this filter, so presumably the forms would be detected, and the scripts would go in the footer.

    Meantime, could you help me with a hack? I've tried this in my theme's functions.php:

    add_filter( 'the_content', 'slt_enqueue_gforms_scripts' );
    function slt_enqueue_gforms_scripts( $content ) {
    	$forms = GFFormDisplay::get_embedded_forms( $content, $ajax );
    	foreach( $forms as $form)
    		GFFormDisplay::enqueue_form_scripts( $form, $ajax );
    	return $content;

    However, it seems that enqueue_scripts can't call get_embedded_forms, because the latter is a private method. Any way to do this?

    Posted 12 years ago on Wednesday August 31, 2011 | Permalink
  2. Hi Steve,

    The problem is "the_content" fires way too late for enqueueing scripts/styles. Enqueueing styles needs to happen before the header is rendered.

    What you can do is print the scripts using the GFFormDisplay::print_form_scripts($form, $ajax); function. For more details, take a look form_display.php circa line 700.

    Posted 12 years ago on Wednesday August 31, 2011 | Permalink
  3. Why can't you have a filter on the_content which would then enqueue with the $in_footer parameter set to true? Then, if some content with a Gform shortcode is placed somewhere after the header has rendered, the scripts would go in the footer.

    Regarding GFFormDisplay::print_form_scripts (line 1175?), do you mean to print the scripts inside the <body> when the_content filter is run? Or to set a flag in the_content, and then print scripts in the footer if the flag is set?

    Posted 12 years ago on Wednesday August 31, 2011 | Permalink
  4. Hi Steve,

    After a certain point, I do not believe it is possible to enqueue a script, even with the $in_footer flag set to true. Feel free to correct me on this if you have a method that works.

    In regards to the print_form_scripts() function, you can do this a couple ways.

    1) Print the scripts inline with the Gravity Form wherever the shortcode is found.
    2) Similarly to your own idea, set a flag if the scripts need to be loaded, then add a hook in the footer to user the print_form_scripts() function that checks the flag and if true, prints the scripts in the footer.

    Posted 12 years ago on Wednesday August 31, 2011 | Permalink