Support

Account

Home Forums Backend Issues (wp-admin) User ID when using update_value on user profile

Solved

User ID when using update_value on user profile

  • I’m setting up an admin approval system where any changes require an admin to update a select field in the user’s profile to ‘approved’. I have two mirrored field groups that are stored on the users profile page. Data is submitted / stored by the user in the first field group, and then upon approval, is mapped to the cloned field group which is what is displayed to the public.

    I have it working to the point that if a user updates their profile, the status field is set to pending, and when I go to the user page in the admin and set it to approved the data is mapped to the other group field.

    The problem is that the approval / update only works if I’m editing my own user profile. If I go to any other user as an admin, and try to update the fields, nothing happens.

    I realized this behavior makes sense as I’m passing the current user ID which is of course me, but I’m not sure how to otherwise get the user’s ID in the filter to be able to update data for that specific user. Is there something obvious here I’m missing, or an easy solution to this? Is my approach a monstrosity?

    Here’s my code:

    function acf_update_status($value, $post_id, $field, $original) {
    	$user = wp_get_current_user();
    	$user_id = 'user_' . $user->ID;
    	$old = get_field('profile', $user_id); // user submitted data
    	$new = get_field('profile_approved', $user_id); // approved data
    	$status = get_field('update_status', $user_id); // profile status
    
    	// map user submitted data to approved data group if approved
    	if ($old != $new && $status == 'approved') :
    		foreach ($old as $key => $val) :
    			update_field('profile_approved_' . $key, $val, $user_id);
    		endforeach;
    		
    		$value = 'approved';
    	endif;
    
    	return $value;
    }
    
    add_filter('acf/update_value/name=update_status', 'acf_update_status', 10, 4);
    
    function acf_update_user_profile($value, $post_id, $field, $original) {
    	$user = wp_get_current_user();
    	$user_id = 'user_' . $user->ID;
    	$old = get_field('profile', $user_id);
    
    	// set to pending if old data is different from submitted data
    	if ($value != $old) :
    		update_field('update_status', 'pending', $user_id);
    	endif;
    
    	return $value;
    }
    
    add_filter('acf/update_value/name=profile', 'acf_update_user_profile', 10, 4);
  • Figured it out, I was stupidly overriding the exact global variable I was supposed to be using.

    Using the global $user_id variable did the trick!

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.