update user metadata

By: Roberta Cerami | Asked: 01/03/2024
ForumsCategory: Code Helpupdate user metadata
Roberta Cerami asked 5 months ago
Hi, I need to update a custom metadata if a field value change. How can I do that? I cannot use the register user action because I cannot update the user role ( I have several ones). Any ideas? I can retrieve the value of the user metadata using shortcode.
2 Answers
Rob LeVineRob LeVine answered 5 months ago
Assuming that "field value change" means the user or admin updates the entry via the front (or back) end, you can use the frm_after_update_entry hook.
Roberta Cerami answered 5 months ago

add_action('frm_after_update_entry', 'update_used_passes', 10, 2);
function update_used_passes($entry_id, $form_id){     
        if ( $form_id == 35 ) {                              
   $userid = $_POST['item_meta'][867];// 867 is the ID of the userID field                         
$used_passes = $_POST['item_meta'][875];// 875 is the ID of the used passes field                 
$pass_status = $_POST['item_meta'][970];// 970 is the ID of the pass status field                                             
  if ( $userid && $pass_status=="Approved") {                         
 // Get the current value.                         
$count = (int) get_field('passes_used', $userid);                         
// Increase it.
$count++;                          
update_field('passes_used', $count, $userid);                       
}             
}}

Roberta Cerami replied 5 months ago

it doesn't work

Rob LeVineRob LeVine replied 4 months ago

Turn on WordPress debugging and use error_log to output information to tell you where it's going wrong. First verify that the function is being called and then output values along the way to see what's happening.

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