Skip to content

Commit beee122

Browse files
committed
Fix token config missing forms rendered via wp_footer
The localized script config was built at wp_footer priority 1, before themes/plugins that render Gravity Forms in modals or popups via wp_footer at default priority. Those forms were never included in the config, so the JS never injected a token for them — causing every submission to be flagged as spam with "did not include a spam prevention token." Replace wp_localize_script (called at wp_footer priority 1) with script_loader_tag, which fires during wp_print_footer_scripts (wp_footer priority 20). This guarantees the config includes all forms collected up to script print time.
1 parent 2c3fb0b commit beee122

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

includes/class-gf-zero-spam.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -276,35 +276,45 @@ public function enqueue_script( $form_string, $form ) {
276276
true
277277
);
278278

279-
// Output config in the footer so all forms have been collected.
280-
add_action( 'wp_footer', [ $this, 'localize_config' ], 1 );
279+
// Inject the config at print time via script_loader_tag rather than
280+
// wp_localize_script in a wp_footer callback. This guarantees all forms
281+
// have been collected because script_loader_tag fires during
282+
// wp_print_footer_scripts (wp_footer priority 20), after themes and
283+
// plugins that render forms via wp_footer at default priority.
284+
add_filter( 'script_loader_tag', [ $this, 'inject_config' ], 10, 2 );
281285

282286
return $form_string;
283287
}
284288

285289
/**
286-
* Passes the collected form configurations to the external script.
290+
* Injects the form configuration inline before the Zero Spam script tag.
287291
*
288-
* Runs in wp_footer so all forms on the page have been processed
289-
* by add_key_field() before the config is serialized.
292+
* Uses script_loader_tag instead of wp_localize_script so the config is
293+
* built at print time, after all forms on the page (including those
294+
* rendered via wp_footer by themes and plugins) have been collected.
290295
*
291-
* @since 1.7.3
296+
* @since TBD
292297
*
293-
* @return void
298+
* @param string $tag The script tag HTML.
299+
* @param string $handle The script handle.
300+
*
301+
* @return string The (possibly modified) script tag HTML.
294302
*/
295-
public function localize_config() {
296-
if ( empty( $this->pending_scripts ) ) {
297-
return;
303+
public function inject_config( $tag, $handle ) {
304+
if ( 'gf-zero-spam' !== $handle || empty( $this->pending_scripts ) ) {
305+
return $tag;
298306
}
299307

300-
wp_localize_script(
301-
'gf-zero-spam',
302-
'gfZeroSpamConfig',
308+
$config = wp_json_encode(
303309
[
304310
'forms' => array_values( $this->pending_scripts ),
305311
'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG,
306312
]
307313
);
314+
315+
$inline = sprintf( "<script type='text/javascript'>var gfZeroSpamConfig = %s;</script>\n", $config );
316+
317+
return $inline . $tag;
308318
}
309319

310320
/**

readme.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ You can enable a spam summary report email. This email will be sent to the email
110110

111111
== Changelog ==
112112

113+
= develop =
114+
115+
* Fixed: Forms rendered in modals or other elements output via `wp_footer` (e.g., site-wide popups, slide-ins) were missing the spam prevention token, causing legitimate submissions to be flagged as spam
116+
113117
= 1.7.4 on April 2, 2026 =
114118

115119
* Added: "Anti-Spam Expiration" setting to control how long spam prevention tokens remain valid, accessible from Forms > Settings > Zero Spam

0 commit comments

Comments
 (0)