Skip Navigation

[Resolved] How to get post id when using toolset form

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 2 replies, has 1 voice.

Last updated by - 18 minutes ago.

Assisted by: Christopher Amirian.

Author
Posts
#2913208

Dear toolset team,

I am using Toolset Forms.

Form name:
business-system-self-diagnosis

Storage destination:
diagnosis-responses

I would like to retrieve the Post ID of the `diagnosis-responses` entry created after the form is submitted and execute some custom PHP.

Questions:

1.
What is the recommended hook to use for executing code after a form submission?

2.
From which parameter can I retrieve the Post ID of the newly created entry?

3.
Is it possible to execute custom PHP immediately after the data is saved—specifically to perform an `update_post_meta()` call?

4.
What is the recommended way to pass the created Post ID when redirecting the user to the "Business System Self-Diagnosis Report" page?

Toolset Forms version:
7.1.4

Regards,

Yoshihiko

#2913465

Christopher Amirian
Supporter

Languages: English (English )

Hello Yoshihiko,

Here are the answers to your questions.

1. Use the "cred_save_data" hook. It runs right after the form saves the post. You can limit it to one form with "cred_save_data_form_" followed by the form slug.

2. The hook receives the new post ID as its first parameter.

3. Yes. The post is already saved when the hook runs, so you can call update_post_meta() there.

4. In the form settings, set "After visitors submit this form" to "Go to a page..." and pick your "Business System Self-Diagnosis Report" page. Then use the "cred_success_redirect" filter to add the post ID to the URL.

Here is an example for your form. Add it to your theme's functions.php or in Toolset > Settings > Custom Code:

add_action( 'cred_save_data_form_business-system-self-diagnosis', function ( $post_id, $form_data ) {
	update_post_meta( $post_id, 'your-meta-key', 'your value' );
}, 10, 2 );

add_filter( 'cred_success_redirect_form_business-system-self-diagnosis', function ( $url, $post_id, $form_data ) {
	return add_query_arg( 'report_id', $post_id, $url );
}, 10, 3 );

On the report page you can read the ID with [wpv-search-term param="report_id"], or with $_GET['report_id'] in PHP.

You can find all the Forms hooks here:
https://toolset.com/documentation/programmer-reference/cred-api/


The custom code suggested above is not part of our official support policy. We will not be able to maintain or guarantee that it will work. It is an attempt to help you understand what should be done for scenarios where the built-in Toolset features are not there to handle your request.

Thanks.

#2913795

Hello, Christopher,

I am very grateful for your precise and prompt response.
I always appreciate how you include additional information that helps me gain a deeper understanding of the issues I need to resolve. Thank you once again for your response.