frm_after_create_entry Hook

By: Johan Potgieter | Asked: 11/18/2024
ForumsCategory: Code Helpfrm_after_create_entry Hook
Johan Potgieter asked 1 month ago
I’m using the Lite version of Formidable Form and from everything I have found I should be able to use the frm_after_create_entry hook. I have added a Newsletter field to my user profile page my own plugin. I’m trying to update this field when a user subscribes to the news letter from the website. If they are logged in the user profile is updated. This is my code but it’s not doing anything.
add_action(\'frm_after_create_entry\', \'joepotgieter_add_entry_id_to_user\', 30, 2);
function joepotgieter_add_entry_id_to_user($form_id) {
if ($form_id == 3) { // my Newsletter form id
// Check if the user is logged in
if (is_user_logged_in()) {
$user_id = get_current_user_id();
error_log(\"User ID: \" . $user_id);

// Update the user meta field
$updated = update_user_meta($user_id, \'newsletter_field\', \'on\');

// Log success or failure
if ($updated) {
error_log(\"Successfully updated user meta for user ID: \" . $user_id);
} else {
error_log(\"Failed to update user meta for user ID: \" . $user_id);
}
} else { //Dont actually care about this as the form sends an email
error_log(\"User is not logged in.\");
}
}
}

Can anyone help with this?
Joe
1 Answers
Rob LeVineRob LeVine answered 1 month ago
What does "it's not doing anything" mean? You have logging statements, are they not being written to the debug file? With any more specifics, I can only guess that update_user_meta isn't updating the field value because the field value doesn't exist. You have to use a combination of add_user_meta and update_user_meta to ensure a value if you're not sure there is one.
Johan Potgieter replied 1 month ago

Hi Rob,

The field is created in another function before in the user's profile. The user I'm testing with has had this field for a while. I can check and uncheck it on the user profile page and it updates the database. Running the above code should (in my understanding) run after the Formidable form (3) has completed. The database is not not updated so I can only assume it's not running. I'm working on a "staging" site and the owners will not give me access to ssh or ftp so I can't view the logs. They claim none of my error_log items are in the error.log.

This is frustrating as you can see my hands are tied on debugging this.

Is this hook available on the Lite version?

Joe

Rob LeVineRob LeVine replied 1 month ago

It sounds like you want frm_after_update_entry, which fires when an entry is updated, rather than created. Once it's created frm_after_create_entry will never be called again.

Johan Potgieter replied 1 month ago

I'll give that a shot. Thanks.

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