Support Forum
Support › MB Frontend Submission › Ajax interfering with using get_the_ID() inside rwmb_frontend_after_process
Hello,
I am doing something very similar to this, but using a MB custom table to save the data: https://metabox.io/how-to-create-a-contact-form-with-mb-frontend-submission/
I am displaying my form on the single pages of a CPT. I want to know which post the form was submitted from, so I am just hooking into rwmb_frontend_after_process
and setting $reference_post_id = get_the_ID();
and then saving that value to the database using the MetaBox\CustomTable\API::update
feature. That way whenever I look at the new post that is created from the form submit, I know which post that submission came from.
This was working perfectly until I decided to turn on ajax for the frontend form. Looks like doing this is causing problems with getting the id of post that the form is on. Is there a way to do this while using ajax?
I already saw this thread, but it also seems to require turning ajax off. Also, I'm not working with relationships.
https://support.metabox.io/topic/prepopulate-relationship-field-with-current-post-id/page/2/
Hi Graceson,
For the post submitted, you can use the passing variable $post_id
to get the post ID. For the current post that shows the frontend form, you can use the WP function get_queried_object_id()
to get the post ID. For example:
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
$data = [
'field_1' => 'value 1',
'field_2' => 'value 2',
'field 3' => ['one', 'two', 'three'],
];
// or \MetaBox\CustomTable\API::update( get_queried_object_id(), 'my-custom-table', $data );
\MetaBox\CustomTable\API::update( $post_id, 'my-custom-table', $data );
}, 10, 2 );
Hi Long,
I don't think this answer quite gets at the problem I was having (which is still unsolved).
What I was trying to say is this. When ajax is turned on, the following no longer works:
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
$reference_post_ID = get_queried_object_id();
$data = [
'reference_post_ID' => $reference_post_ID,
'field_2' => 'value 2',
'field 3' => ['one', 'two', 'three'],
];
\MetaBox\CustomTable\API::update( $post_id, 'my-custom-table', $data );
}, 10, 2 );
The idea is that I want to save the ID of the current post where the form is displayed to the post that is being submitted by the user. That way, I can track which post each submission came from. This works with [mb_frontend_form id="field-group-id" ajax="false"]
but it fails when [mb_frontend_form id="field-group-id" ajax="true"]
Thanks,
Graceson
Hi,
Thanks for your feedback.
I see the field value is updated as well but somehow the function get_queried_object_id()
does not return the correct post/page ID that displays the frontend form. I will inform the development team to check this issue and get back to you later.
Hi Long,
Have you heard back from the development team on this issue?
Thanks,
Graceson