Using frm_after_create_entry Hook to update form entry ownership

By: Steve Dussemberg | Asked: 07/21/2025
ForumsCategory: Code SnippetsUsing frm_after_create_entry Hook to update form entry ownership
Steve Dussemberg asked 5 months ago

I'm trying to update the User ID for a form entry filled by another user who can select a new user through a dynamic field user list

The following code should do just that but it does not update the user ownership.

Can someone take a look at this and give me some guidance.

Much appreciated

add_action('frm_after_create_entry', 'assign_entry_owner_from_field', 20, 2);
function assign_entry_owner_from_field($entry_id, $form_id) {
if ($form_id != 6) { // 🔁 Replace 6 with your form ID
return;
}

// Replace 123 with your Dynamic Field ID
$field_id = 123;

// Retrieve value from the form entry
$entry = FrmEntry::getOne($entry_id, true);
if (!isset($entry->metas[$field_id])) {
error_log("Field $field_id not found in entry $entry_id.");
return;
}

$new_user_id = intval($entry->metas[$field_id]);

// Validate the user
if (!get_userdata($new_user_id)) {
error_log("User ID $new_user_id is not valid.");
return;
}

// Update entry ownership
global $wpdb;
$updated = $wpdb->update(
$wpdb->prefix . 'frm_items',
['user_id' => $new_user_id],
['id' => $entry_id]
);

if ($updated === false) {
error_log("Failed to update entry owner for entry $entry_id.");
} else {
error_log("Successfully assigned entry $entry_id to user $new_user_id.");
}
}

Attachments

1 Answers
Rob LeVineRob LeVine Staff answered 5 months ago

I'll start by assuming you changed all the values to the correct ones in your form (e.g., 6, 123). From there, I see that the line that's updating a user ID, is not doing what you think it is. You want to set the value for the user ID field in the form. To do this, you need to go to your form, retrieve the ID or key of the UserID field, and set that value in your code.
If that's not it, I suggest adding more error_log commands to your code and verify that all the values, especially new_user_id and entry_id, are correct.

Rob LeVineRob LeVine
Staff replied 5 months ago

Apparently, it's best to do both (I've only updated the userid field in the entry). I see in Formidable's PHP Examples page the following lines of code:
$wpdb->update( $wpdb->prefix .'frm_items', array( 'user_id' => get_current_user_id() ), array( 'id' => $new_entry_id ) );
$wpdb->update( $wpdb->prefix .'frm_item_metas', array( 'meta_value' => get_current_user_id() ), array( 'item_id' => $new_entry_id, 'field_id' => $user_id_field ) );

Steve Dussemberg
replied 5 months ago

Rob thanks for the reply and suggestion. I added those lines to the code but still no working. Just to clarify my form ID is 6 and the dynamic dropdown field ID in my case is 123.

I added an alert to show the new user id and the entry id and they do match what it suppose to be but it does not update the user id.

Here is the adapted code now:

add_action('frm_after_create_entry', 'assign_entry_to_selected_user', 30, 2);

function assign_entry_to_selected_user($entry_id, $form_id) {
// Replace 123 with your actual form ID
if ($form_id == 6) {
// Get the entry object
$entry = FrmEntry::getOne($entry_id, true);

// Replace xxxxx with the field key or ID of the user dropdown
$user_id = $entry->metas['123'];

if ($user_id && is_numeric($user_id)) {
// Update post author (entry owner)
global $wpdb;
$wpdb->update( $wpdb->prefix .'frm_items', array( 'user_id' => get_current_user_id() ), array( 'id' => $entry ) );
$wpdb->update( $wpdb->prefix .'frm_item_metas', array( 'meta_value' => get_current_user_id() ), array( 'item_id' => $entry, 'field_id' => $user_id ) );

}
echo "alert('$user_id');";
echo "alert('$entry_id');";

if ($wpdb === false) {
error_log("Failed to update entry owner for entry $entry_id.");
echo 'alert("Failed to update entry owner for entry")';
} else {
error_log("Successfully assigned entry $entry_id to user $user_id.");
}
}
}

Any help is much appreciated

Rob LeVineRob LeVine
Staff replied 5 months ago

I can't emphasize enough how putting in more echos or error_log statements will give you the information you need and, quickly. One thing that I notice is that in the second wpdb->update call you've set "field_id" to user_id" and that is not correct. The right-side value should be the id of the user ID field. Moreover, where you're using get_current_user_id(), you should be using $user_id, otherwise, you're just setting the user ID value to what it already is: the id of the user that's logged in.

$wpdb->update( $wpdb->prefix .'frm_items', array( 'user_id' => $user_id), array( 'id' => $entry ) );
$wpdb->update( $wpdb->prefix .'frm_item_metas', array( 'meta_value' => $user_id ), array( 'item_id' => $entry, 'field_id' => ) );

Steve Dussemberg
replied 4 months ago

Hi Rob,

I've been busy with other tasks jut now returning to this project.

Here the the current code:

add_action('frm_after_create_entry', 'assign_entry_to_selected_user', 30, 2);
function assign_entry_to_selected_user($entry_id, $form_id) {

// 6 is the form ID
if ($form_id == 6) {
// Get the entry object
$entry = FrmEntry::getOne($entry_id, true);

// 123 is the ID of the dynamic field user dropdown in the form
$user_id = $entry->metas['123'];

// 136 is the ID of the User ID field in the form
$user_id_field = FrmField::get_id_by_key('yjd45'); // gets User ID field id returns 136 which is correct

if ($user_id && is_numeric($user_id)) {
// Update post author (entry owner)
global $wpdb;
$wpdb->update( $wpdb->prefix .'frm_items', array( 'user_id' => $user_id), array( 'id' => $entry ) );
$wpdb->update( $wpdb->prefix .'frm_item_metas', array( 'meta_value' => $user_id ), array( 'item_id' => $entry, 'field_id' => $user_id_field ) );
}

echo "alert('$user_id');"; // $user_id is returning 145 which is the ID of the user selected and it is correct

echo "alert('$entry_id');"; // $entry_id is returning the correct Entry ID for each time the form is used

echo "alert('$user_id_field');"; // $user_id_field is returning the correct User Id field ID in this case 136

if ($wpdb === false) {
error_log("Failed to update entry owner for entry $entry_id.");
echo 'alert("Failed to update entry owner for entry")';
}
else {
error_log("Successfully assigned entry $entry_id to user $user_id.");
}
}
}

All the echo returns are correct so it does get the right information but still it does not update the user. Why????

Any help is much appreciated

Rob LeVineRob LeVine
Staff replied 4 months ago

I'll give it one more try, then suggest you hire a developer to fix the issue. Three things that I see that are not correct are: 1. You can't set a value to $entry in this case. $entry is an object. You want $entry->id, though I don't know why you need to do the $entry = FrmEntry::getOne, since the $entry_id is already passed in. 2. $wpdb will never equal false, so that if statement will never result in true. What you want to look at is what value $wpdb->update returns, for each case. 3. (not important but..) In the echo commands, you don't need "alert()". "alert" is a JavaScript thing, so it adds nothing to your output beyond extra text.

Steve Dussemberg
replied 4 months ago

Thank you Rob, I appreciate the effort. Since I'm not a PHP expert myself I'm doing the best I can. I'll not gave up on the project. I'll learn more if I have to until I get this thing done. Thanks again for your time.

Steve Dussemberg
replied 4 months ago

<p>Just before you replied I was trying a different approach just so you know. add_action('frm_after_create_entry', 'assign_entry_to_selected_user', 30, 2); function assign_entry_to_selected_user($entry_id, $form_id) { if ($form_id == 6) { $entry = FrmEntry::getOne($entry_id, true); // Get the selected user ID from the dynamic dropdown (Field ID 123) $user_id = $entry->metas['123']; // Get the hidden user ID field key and resolve it to ID $user_id_field = FrmField::get_id_by_key('yjd45'; // Should be 136 if ($user_id && is_numeric($user_id)) { global $wpdb; // Fix: use $entry_id instead of $entry $wpdb->update( $wpdb->prefix . 'frm_items', array('user_id' => $user_id), array('id' => $entry_id) ); $wpdb->update( $wpdb->prefix . 'frm_item_metas', array('meta_value' => $user_id), array('item_id' => $entry_id, 'field_id' => $user_id_field) ); // Debug logs error_log("Successfully assigned entry $entry_id to user $user_id."; } else { error_log("Invalid user ID for entry $entry_id."; } } }</p><p>Now the User ID Entry is empty. I'll figure it out</p>

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