How do I get a Post ID into a form field before an API action is triggered?

By: David Silvester | Asked: 08/08/2024
ForumsCategory: Code HelpHow do I get a Post ID into a form field before an API action is triggered?
David SilvesterDavid Silvester asked 5 months ago
Hi team, I'm using Formidable forms to create and edit two different types of posts, "Studios" and "Artists". I need help passing the post_id of the Studio to a field (Studio Post ID) in the Artist post. Here's what I've got set up and working so far:
  1. Form to create studio.
  2. Form action Create Post to create the Studio post
  3. Using the hook frm_after_create_entry to populate a field in my studio form entry with the Post ID, as per this example.
  4. Using Formidable's API action to pass data from the Create Studio form to a Create Artist form, which then creates the Artist post automatically.
My problem is that the hook frm_after_create_entry seems to run after the API action, so when the API action sends Studio entry data to the Create Artist form the Studio Post ID field is blank. I've tried using the frm_api_action_options filter to make the API action happen after the Create Post action as per this example but it hasn't worked either. I'm wondering if changing the priority number will help? (I don't have a clear understanding about how priority numbers work in WordPress actions.) The goal of all this is to populate an ACF Relationship field on the Artist post that links the Artist to the correct Studio post. Anyone got any other ideas on how to approach this?
1 Answers
David SilvesterDavid Silvester answered 5 months ago
As is often the case, I think I've solved this one for myself! The solution I found was to abandon using the Formidable API method of creating a submission in my second form and just use the frm_after_create_entry hook instead. I started with this code example and then modified it thus:
add_action('frm_after_create_entry', 'create_entry_after_submission', 60, 2);

function create_entry_after_submission($entry_id, $form_id){
if ( $form_id != 2 ) { //ID of Form A
return;
}
//Get the initial form entry and find the post_id of the post created by this entry.
$entry = FrmEntry::getOne( $entry_id );
if ( ! $entry->post_id ) {
return;
}

$form2 = '4'; //ID of Form B
//Get user
if (!isset($_POST['frm_user_id'])){
return;
}
$user = $_POST['frm_user_id'];
//get data
if (!isset($_POST['item_meta'][84])){ //84 is the field ID of the Form A value
return;
}

$studio_post_id = $entry->post_id; //assign the post_id to this variable
$artist_name = $_POST['item_meta'][84];
$artist_description = $_POST['item_meta'][229];
//create entry
FrmEntry::create(array(
'form_id' => $form2,
'item_key' => $user . $entry_id,
'frm_user_id' => $user,
'item_name' => $artist_name,
'item_meta' => array(
171 => $user,
225 => $studio_post_id,//Put the post_id into the correct field of the new form entry
98 => $artist_name,
121 => $artist_description,
),
));
}

I'm a novice coder so I've just hacked this together until it worked. If any boffins out there can see any issues or any ways to improve it I'd be much obliged!

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right