Where Do I Put This Code?

Introduction

Depending on what code you are trying to add, and the complexity of the theme you are using, there can be a number of different places that code snippets can be added to extend your forms. This article covers most of the common options.

Important!

Replacing or updating your theme can overwrite the theme directory in your site folder, and that means modifications like these you may have made to files there will be lost. Always keep regular back-ups, consider the use of a child theme, and check out some of the third party plugins that provide safer alternatives for adding code.

CSS

CSS code will typically look something like this:

body .gform_wrapper { border:1px solid red }

Please see this article for placement options.

JavaScript/jQuery

JavaScript or jQuery code will typically look something like this:

jQuery(document).ready(function($) {
    $('.gfield_checkbox input').click(function(){
        var is_checked = $(this).is(':checked');
        var parent = $(this).parent('li');
        if(is_checked){
            $(parent).addClass('selected');
        } else {
            $(parent).removeClass('selected');
        }
    });
});

Community Library Alternative
The team at Gravity Wiz have a free Custom JavaScript add-on which you can use to include custom scripts for forms.
(see disclaimer at bottom of article)

Some themes will provide an init.js or, even better, a custom.js file. If so, pasting the custom JavaScript in either of these files would be ideal. If neither is present, you can wrap your JavaScript code in a <script> block and paste the code in your theme’s header.php file below the wp_head() function call.

<script type="text/javascript">
    jQuery(document).ready(function($) {

        // custom code

    });
</script>

Additional Information on using JavaScript with WordPress can be found here: https://codex.wordpress.org/Using_Javascript

PHP

PHP code will typically look something like this:

<?php
function gform_add_post_format($entry) {

    $post_id = rgar($entry, 'post_id');

    if(!$post_id)
        return;

    wp_set_object_terms($post_id, 'chat', 'post_format');

}
?>

Theme’s functions.php file

PHP snippets will almost always be pasted in your theme’s functions.php file, after any other existing code or right after the opening <?php tag.

A few things to be aware of when adding PHP snippets.

  1. Your functions.php will already have an opening <?php tag. If the snippet you are copying also has an opening PHP tag, be sure to remove it before pasting it into your functions.php file.
  2. If the PHP snippet you are copying has an opening and closing PHP tag inside the code itself, do not remove these. This simply means that the code is exiting “PHP mode” to output content directly to the screen.
  3. If you update your theme, you will need to add the snippet again. To avoid this situation, please use a child theme.

Third Party Plugins

Some enterprising WordPress developers have released third party plugins to provide safer methods for adding code. Check out the WordPress repository to see what is available.

Here is one that is widely used: Code Snippets Third Party Plugin

Pluginception is another option to consider, it allows you to create custom functionality plugins easily from your WordPress dashboard.

Disclaimer: Third-party services, plugins, or code snippets that are referenced by our Support documentation or in Support Team communications are provided as suggestions only. We do not evaluate, test or officially support third-party solutions. You are wholly responsible for determining if any suggestion given is sufficient to meet the functional, security, legal, ongoing cost and support needs of your project.

Feedback, feature and integration requests, and other functionality ideas can be submitted on our Gravity FormsGravity Flow, or Gravity SMTP product roadmap pages.