What is Custom Code Keeper for Gravity Forms?

If you regularly employ snippets of PHP code in customizing Gravity Forms, then Custom Code Keeper for Gravity Forms will serve you well. Let’s take a look at why this code keeper is needed and how it can benefit you.

The Usual Approach

Up to now, the usual method for running custom form-related code on a site is to either:

  1. Add snippets to a theme’s functions.php. (Note that themes aren’t generally accepted as a place for site functionality and adding code to a primary theme also makes it prone to loss during theme updates.)
  2. Install a code snippet plugin. (Loading PHP code from database queries? – meh)
  3. Spin up a plugin to house custom code. (This is a very suitable method, but can also become unnecessarily complex when including lots of code – especially when that code is intended for a wide variety of purposes.)

These methods have inadequacies. Custom Code Keeper for Gravity Forms was created to address such concerns and provide a more reliable solution for organizing and loading your custom form-related code.

What Makes Custom Code Keeper Different?

By providing an orderly environment for hosting your code, Custom Code Keeper for Gravity Forms gives you a better sense of the specific code running across your site. Having your code reside outside a specific plugin or theme ensures your hard work is not lost to theme updates or plugin removal. The file-per-form framework encourages orderly code that helps you identify, access, investigate, and update code much more readily. And, if you need to take any of your code to another site, the code file export features make it easy to pull just the code you need.

How Does It Work?

Upon installation, the plugin will create the directory structure wp-content/uploads/gravity_hopper/code/ on your site. The first thing to note is that all code from allowed files residing in this directory will run for all forms. Custom Code Keeper for Gravity Forms is intended for code organization only and doesn’t restrict when code is run.

Always use appropriate hooks and/or conditional checks when targeting specific forms and fields.

After setting up the directory structure, the plugin will populate the directory with two files. The first is named gf-global-code.php and is intended to house code that is to be used across your Gravity Forms install – this might include custom classes and functions that will be called upon throughout your codebase. The second is named gform-00xx.php and is intended as an example of the form files you will see when you begin generating files for organizing your code. Note that each file associated with a form will be named in this conventional manner (including the zeroes to pad the form ID). Also note that each form file will only be loaded if the form matching the ID exists on the site.

To automate the creation and deletion of files and to load other files, see the available filters below.

When a form has an associated file, you will be able to preview the file contents (and view the absolute file path) from the Form Settings → Code page in the Gravity Forms interface. If the form does not have an associated file, you will be able to generate a blank file for the form from that same page.

Should you want to extract your code for use on another site, the plugin allows you to download any or all code files in a zipped folder from the Forms → Import/Export → Export Code screen.

Advanced Usage


add_filter( 'gravityhopper-cck/create_file_after_new_form', '__return_true' );
add_filter( 'gravityhopper-cck/create_file_after_duplicate_form', '__return_true' );
add_filter( 'gravityhopper-cck/remove_file_after_delete_form', '__return_true' );

These three filters change the behavior of file auto-creation/duplication/deletion when managing forms. They are included and commented in the file gf-global-code.php. Simply uncomment them to enable automatic file creation or removal as forms are created and deleted from the site.

You may like to include your own custom files. For example, if you should employ any of the magical Gravity Wiz snippets, you may want to load files prefixed with gw- and gp-. You might also have your own agency code with which you’d like to prefix certain files. The filter gravityhopper-cck/allowed_file_prefixes lets you add those prefixes to an allow list. Use it like so:

add_filter( 'gravityhopper-cck/allowed_file_prefixes', function( $allowed_prefixes ) {

    $allowed_prefixes[] = 'gw';
    $allowed_prefixes[] = 'gp';
    $allowed_prefixes[] = 'typewheel';

    return $allowed_prefixes;

} );

Now with that snippet in place, all files prefixed with gw or gp or typewheel that reside in your gravity_hopper/code/ directory would also be loaded.

Loading of Files

The order in which files are loaded is as follows:

  1. gf-global-code.php
  2. files associated with each prefix allowed via gravityhopper-cck/allowed_file_prefixes
    (prefixes run according to the order in which they’ve been keyed and files are loaded alphabetically)
  3. form files prefixed with gform- incrementally according to the associated form ID.

In rare situations, you may want to remove the plugin Custom Code Keeper for Gravity Forms, but allow all your code to still be run on the site. In that case, you can copy the file files/gravityhopper-custom-code-keeper-loader.php from the plugin to your mu-plugins directory. This will allow your custom Gravity Hopper code to load even when this plugin is deactivated or removed from the site. At that point, other features of the plugin would not function – only your custom code would load. Note that this is not generally advised as it can hinder troubleshooting efforts on the site.

Take Flight

It is my hope that this plugin will serve to improve your development environment – saving you time and effort both now and when need to revisit the forms you’ve built.

We welcome feedback and questions. Drop us a note via our contact form or connect with us @gravityhopper.

If you’re ready to level up your Gravity Forms custom code game, download Custom Code Keeper for Gravity Forms today.