Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions gp-nested-forms/gpnf-gpeb-auto-attach-child-entries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Gravity Perks // Nested Forms // Auto-attach Child Entries to Parent when Editing via GP Entry Blocks.
* https://gravitywiz.com/documentation/gravity-forms-nested-forms/
*
* By default, when adding a new child entry to a Nested Form field while editing a parent entry via GP Entry Blocks, the
* child entry is saved to the session and will not be attached to the parent entry unless you click "Update" on the
* parent entry form.
*
* Use this snippet to automatically attach the child entry to the parent as soon as the child form is submitted.
*/
add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) {
if ( rgget( 'edit_entry' ) ) {
$parent_entry_id = rgget( 'edit_entry' );
}
return $parent_entry_id;
} );
Comment on lines +12 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Sanitize and type-cast edit_entry; avoid double rgget to prevent accidental 0 parent IDs

Cast to int once and reuse. This prevents non-numeric values from coercing to 0 and unintentionally overriding the parent ID.

Apply this diff:

-add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) {
-	if ( rgget( 'edit_entry' ) ) {
-		$parent_entry_id = rgget( 'edit_entry' );
-	}
-	return $parent_entry_id;
-} );
+add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) {
+	$edit_entry = absint( rgget( 'edit_entry' ) );
+	if ( $edit_entry ) {
+		$parent_entry_id = $edit_entry;
+	}
+	return $parent_entry_id;
+} );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) {
if ( rgget( 'edit_entry' ) ) {
$parent_entry_id = rgget( 'edit_entry' );
}
return $parent_entry_id;
} );
add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) {
$edit_entry = absint( rgget( 'edit_entry' ) );
if ( $edit_entry ) {
$parent_entry_id = $edit_entry;
}
return $parent_entry_id;
} );
🤖 Prompt for AI Agents
In gp-nested-forms/gpnf-gpeb-auto-attach-child-entries.php around lines 12 to
17, the current code calls rgget('edit_entry') twice and does not
sanitize/type-cast the value; change it to read rgget('edit_entry') once into a
variable, check if it is non-empty, cast it to (int) and then assign to
$parent_entry_id only when the cast result is > 0 to avoid accidentally
overriding a valid parent ID with 0 from non-numeric input.

💡 Verification agent

🧩 Analysis chain

Verify “edit_entry” is the correct GP Entry Blocks param across this repo

Double-check consistency with existing snippets (e.g., gpeb-remove-fields-on-edit.php) and usages to avoid mismatched query params.

Run this script to confirm:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Searching for existing snippets referencing edit_entry…"
rg -n "rgget\(\s*'edit_entry'\s*\)" -A 2 || true

echo
echo "Locating gpeb-remove-fields-on-edit.php (if present) and printing its contents…"
fd -i -a gpeb-remove-fields-on-edit.php | while read -r f; do
  echo "---- $f ----"
  sed -n '1,200p' "$f"
done

echo
echo "Search for query param name patterns possibly used by Entry Blocks:"
rg -n "(edit_entry|edit\-entry|entry_id|entry-id|eid)" -A 2 || true

Length of output: 96754


Standardize on the GP Entry Blocks query parameter

The only occurrence of rgget('edit_entry') is in gp-nested-forms/gpnf-gpeb-auto-attach-child-entries.php, but all other Entry Blocks snippets expect the standard gpeb_entry_id parameter. Update this hook to read the correct param (or support both for backward compatibility).

Locations to update:

  • gp-nested-forms/gpnf-gpeb-auto-attach-child-entries.php (lines 12–17)

Proposed change:

 add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) {
-   if ( rgget( 'edit_entry' ) ) {
-       $parent_entry_id = rgget( 'edit_entry' );
-   }
+   // Use the standard GP Entry Blocks parameter
+   $entry_id = rgget( 'gpeb_entry_id' ) ?: rgget( 'edit_entry' );
+   if ( $entry_id ) {
+       $parent_entry_id = $entry_id;
+   }
    return $parent_entry_id;
} );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) {
if ( rgget( 'edit_entry' ) ) {
$parent_entry_id = rgget( 'edit_entry' );
}
return $parent_entry_id;
} );
add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) {
// Use the standard GP Entry Blocks parameter
$entry_id = rgget( 'gpeb_entry_id' ) ?: rgget( 'edit_entry' );
if ( $entry_id ) {
$parent_entry_id = $entry_id;
}
return $parent_entry_id;
} );
🤖 Prompt for AI Agents
In gp-nested-forms/gpnf-gpeb-auto-attach-child-entries.php around lines 12 to
17, the hook currently reads rgget('edit_entry') but other Entry Blocks use the
standard gpeb_entry_id param; change the logic to read gpeb_entry_id first and
fall back to edit_entry for backward compatibility (i.e., if
rgget('gpeb_entry_id') is present use that, otherwise use rgget('edit_entry')),
then return the resolved parent_entry_id.

Loading