Home › Forums › Backend Issues (wp-admin) › 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!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.