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.

wp_head include

  1. Heya guys

    When inserting the form via a template tag (or any other method other than directly into a page), the JS and CSS for GForms isn't included in the head.

    Could you perhaps include a method of a) option to always include gforms stuff in the head, or b) detect usage of the template tag & include head stuff...

    Either way...
    Thanks :)

    Posted 9 years ago on Monday November 2, 2009 | Permalink
  2. *bump :)

    Posted 9 years ago on Tuesday November 17, 2009 | Permalink
  3. d4le
    Member

    I added this to the functions.php file in my template..

    if(function_exists('gravity_form')){
          add_action('wp_print_styles', 'gforms_style');
          function gforms_style(){ wp_enqueue_style("gforms_css", GFCommon::get_base_url() . "/css/forms.css"); }
        }
    Posted 9 years ago on Tuesday November 17, 2009 | Permalink
  4. Mark,
    I will take a look into this. The scripts and css are supposed to be included if you use the template tags.

    Posted 9 years ago on Tuesday November 17, 2009 | Permalink
  5. d4le
    Member

    Alex,

    Since you're using conditionals to check for the presence of the form inside your plugin, and only then using the enqueue functions, the template tag has to happen BEFORE the instantiation of the wordpress query object for the enqueue to work. So if your template tag is in the loop or after 'the content', it ain't gonna happen.

    Solution, create a template tag that returns the html of a form as an html escaped string, but doesn't echo it like the current one does.

    Before the loop, add $my_form = get_gravity_form(1,false,false);

    after the loop, echo $my_form; the scripts/css will be output.

    I've confirmed that putting gravity_form(1,false,false) before the loop will output the script, after the loop, no (aforementioned reasons).

    Posted 9 years ago on Tuesday November 17, 2009 | Permalink
  6. d4le
    Member

    That was easy, I just did it in five minutes:

    add this function to the gravityforms.php (hacking the core, I know):

    function get_gravity_form($id, $display_title=true, $display_description=true, $display_inactive=false, $field_values=null){
        return RGForms::get_form($id, $display_title, $display_description, $display_inactive, $field_values);
    }

    Then in your template before an instantiation of a wp query object (ie the loop),

    $my_form = get_gravity_form(1,false,false);

    Then in or after the loop (or whatever query object), add an

    <?php echo $my_form ?>

    Posted 9 years ago on Tuesday November 17, 2009 | Permalink
  7. d4le,
    Thanks for reply. You are absolutely right. If you call the function too late in the life-cycle, the proper scripts won't be printed properly.

    Your solution is certainly a good one. I will talk to the rest of the team and see how we want to tackle it.

    Posted 9 years ago on Tuesday November 17, 2009 | Permalink
  8. d4le
    Member

    One more thing, making this a must-have if you want to output a form in a wordpress hook,

    add_filter ('the_content', 'insertForm');
    function insertForm($content) {
            if(is_single()) {
                    $content.= get_gravity_form(1,false,false);
            }
            return $content;
    }

    You can see how the hook can get even more specific.

    Posted 9 years ago on Wednesday November 18, 2009 | Permalink
  9. d4le
    Member

    I've done it with the_content hook as I posted above, it seems like the gf enqueue functions should work, because it's getting called before the wp_head(), (I assume). But it doesn't!

    I wonder if the problem is in the wp hook, or if it's something you could check for in your program.

    Posted 9 years ago on Wednesday November 18, 2009 | Permalink