Lots of topics from this forum show up when searching Google for a variation on gform.addAction is not a function
(like this one), so I figured I’d post in the same forum with a solution that worked in my particular case. TL;DR I think the team should take another look at the admin script dependencies.
The Problem
The form edit screen loads a gforms_hooks.js
file and inserts it as an inline script attached to the gform_gravityforms
script enqueue with id gform_gravityforms-js-before
. This script begins with if ( ! gform ) { ...
, so if gform
has been defined in the window scope already this script will not add the addAction
and other functions that additional scripts rely on. However, at least in my case, a script with id gform_gravityforms_libraries
is loaded many lines before the inline script, which adds to the global gform
object, but also creates it if it doesn’t exist. Therefore, the global gform
var exists and the hook functions are not added.
I don’t know what specific plugin might be the “conflict” here but I don’t believe that this is a plugin conflict problem, it’s a Gravity Forms code problem that, at least in my case, was easily solved with a single line change in the plugin code. The issue is that the script dependencies for the form editor (at least for these 2 specific scripts) are non-linear, so it is possible for them to load out-of-order depending on what other theme and plugin scripts are enqueued.
The Solution
If the team wants to fix the issue for probably everyone having this problem, simply add gform_gravityforms
as a dependency to the gform_gravityforms_libraries
script registration. That’s line 3110 of gravityforms.php (v2.9.10).
For the rest of us, since editing plugin code is a bad idea, here’s a simple snippet that adds the dependency and fixes the editor.
/**
* Add `gform_gravityforms` as a dependency of `gform_gravityforms_libraries`
* script to ensure it loads first. Fixes a JS error in the form editor that
* certain hook-related functions are not defined, because the GravityForms
* scripts may not be enqueued in the correct order.
*/
add_action(
'init',
function () {
global $wp_scripts;
$libraries_script = $wp_scripts->query( 'gform_gravityforms_libraries', 'registered' );
if ( $libraries_script ) {
$libraries_script->deps[] = 'gform_gravityforms';
}
},
11 // Current priority of GForms script registration is 10.
);