Obtain entry_id from form submission

This must be simpler than I think, but I can’t figure it out. I want to run a php function that just needs the entry_id of the form that was submitted. How do I call the function to pass that entry_id to it when the form submits?

I thought it would be:

add_action( ‘gform_after_submission_146’, ‘set_post_content’, 10, 2 );
function set_post_content( $entry, $form ) {
$post = get_post( $entry[‘entry_id’] );
add_family_member( $post );
}

Hi Dottie,
It looks like you’re trying to use the gform_after_submission_146 hook to run your set_post_content function after submitting a Gravity Form with an ID of 146. The gform_after_submission_146 hook passes two arguments to the function it runs: the $entry array, which contains information about the form submission, and the $form array, which contains information about the form itself.

To access the entry ID of the form submission, you can use the entry_id key of the $entry array. Here’s an example of how you could use it in your function:

add_action( 'gform_after_submission_146', 'set_post_content', 10, 2 );

function set_post_content( $entry, $form ) {
    $entry_id = $entry['entry_id'];
    add_family_member( $entry_id );
}

You can then use the $entry_id variable to pass the entry ID to your add_family_member function.

I hope this helps! Let me know if you have any further questions.

It gives me the error:

[26-Dec-2022 17:24:10 UTC] PHP Warning: Undefined array key “entry_id” in /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-content/themes/astra/functions.php on line 206

line 206: is the line function set_post_content( $entry, $form )

add_action( ‘gform_after_submission_146’, ‘set_post_content’, 10, 2 );

function set_post_content( $entry, $form ) {
$entry_id = $entry[‘entry_id’];
add_family_member( $entry_id );
}

Here’s the whole error log:

[26-Dec-2022 17:35:42 UTC] PHP Warning: Undefined array key “entry_id” in /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-content/themes/astra/functions.php on line 206
[26-Dec-2022 17:35:42 UTC] PHP Fatal error: Uncaught Error: Call to undefined function add_family_member() in /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-content/themes/astra/functions.php:207
Stack trace:
#0 /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-includes/class-wp-hook.php(310): set_post_content(Array, Array)
#1 /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters(‘’, Array)
#2 /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#3 /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-content/plugins/gravityforms/gravityforms.php(7059): do_action(‘gform_after_sub…’, Array, Array, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
#4 /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-content/plugins/gravityforms/form_display.php(185): gf_do_action(‘gform_after_sub…’, Array, Array)
#5 /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-content/plugins/gravityforms/gravityforms.php(844): GFFormDisplay::process_form(146, 1)
#6 /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-includes/class-wp-hook.php(308): GFForms::maybe_process_form(Object(WP))
#7 /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters(NULL, Array)
#8 /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-includes/plugin.php(565): WP_Hook->do_action(Array)
#9 /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-includes/class-wp.php(797): do_action_ref_array(‘wp’, Array)
#10 /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-includes/functions.php(1332): WP->main(‘’)
#11 /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-blog-header.php(16): wp()
#12 /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/index.php(17): require(‘/chroot/home/ac…’)
#13 {main}
thrown in /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/wp-content/themes/astra/functions.php on line 207
[26-Dec-2022 17:35:42 Etc/UTC] PHP Warning: Undefined array key “form_id” in /chroot/home/ac2062f0/8b1f145e25.nxcli.net/html/home-base/user_register.php on line 3

I think I see the issue. I think it should be:

add_action( ‘gform_after_submission_146’, ‘set_post_content’, 10, 2 );

function set_post_content( $entry, $form ) {
$entry_id = $form[‘entry_id’];
add_family_member( $entry_id );
}

That didn’t work, either. I must need some other action. I will try a different way to get what I need as I can’t figure out how to get the form’s entry_id.

It looks like the $entry array does not contain an entry_id key. This could be because the gform_after_submission_146 hook is not running or because the $entry array is not being passed to the hook correctly.

Here are a few things you can try to troubleshoot the issue:

  1. Make sure that the Gravity Form with an ID of 146 is active and configured correctly on your WordPress site.

  2. Check the error log to see if there are any other error messages that might give you more information about the issue.

  3. Make sure your set_post_content function is registered correctly with the gform_after_submission_146 hook. You can do this by adding some debug code to your function like this:

add_action( 'gform_after_submission_146', 'set_post_content', 10, 2 );

function set_post_content( $entry, $form ) {
    error_log( 'set_post_content function called' );
    $entry_id = $entry['entry_id'];
    add_family_member( $entry_id );
}

Then, check your error log to see if the set_post_content function called message is being logged when the form is submitted. If it is not, the hook is not running, and you will need to investigate why.

  1. Make sure that the $entry array contains the data you expect it to. You can do this by adding some debug code to your function like this:
add_action( 'gform_after_submission_146', 'set_post_content', 10, 2 );

function set_post_content( $entry, $form ) {
    error_log( 'Entry data: ' . print_r( $entry, true ) );
    $entry_id = $entry['entry_id'];
    add_family_member( $entry_id );
}

Then, check your error log to see if the $entry array contains the data you expect it to. If it does not, then you will need to investigate why.

I hope these suggestions help! Let me know if you have any further questions.

Thanks for your help! These are great ideas!

The entry id is stored in the $entry using id as the key, so you would access it like so:

$entry_id = rgar( $entry, 'id' );
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.