Home › Forums › ACF PRO › Front-end Update User Meta ACF / WP User Meta Sync › Reply To: Front-end Update User Meta ACF / WP User Meta Sync
OK, got it.
There are a couple of issues here, including zero validation.
My Form:
<?php
acf_form_head();
$avatar_group = get_field( 'sys_avatar_field_group', 'option' );
$current_user = wp_get_current_user();
$user_id = get_current_user_id();
$user_acf_prefix = 'user_';
$user_id_prefixed = $user_acf_prefix . $user_id;
$user_info = get_userdata($user_id);
$member_email = $user_info->user_email;
$options = array(
'post_id' => 'user_'.$current_user->ID,
'field_groups' => array(group_5e880597057c0, $avatar_group),
'submit_value' => 'Update Profile'
);
?>
Validation:
/** User Meta Update Validation ------------------------------------------------ **/
add_action('acf/validate_save_post', 'validate_user_input_data');
function validate_user_input_data() {
$email = $_POST['acf']['field_5e880d5caa23d'];
$user_info = get_userdata($user_id);
$current_email = $user_info->user_email;
if ( empty($_POST['acf']['field_5e880d5caa23d']) ) {
acf_add_validation_error( 'acf[field_5e880d5caa23d]', 'You need to specify an email address' );
}
elseif ( email_exists($email) && $email != $current_email ) {
acf_add_validation_error( 'acf[field_5e880d5caa23d]', 'The email address you entered is not available' );
}
}
Update function:
/** User Meta Update ------------------------------------------------ **/
function update_member_data( $post_id ) {
if( empty($_POST['acf']) ) {
return;
}
if( is_admin() ) {
return;
}
if( $_POST['post_id'] != 'new' ) {
$memberEmail = $_POST['acf']['field_5e880d5caa23d'];
$user_id = str_replace("user_", "", $post_id);
$args = array(
'ID' => $user_id,
'user_email' => esc_attr($memberEmail)
);
wp_update_user($args);
$memberFirstname = $_POST['acf']['field_5e880c9b0e92d'];
$memberLastname = $_POST['acf']['field_5e880d0daa23b'];
$memberNickname = $_POST['acf']['field_5e880d3baa23c'];
$memberDisplayname = $_POST['acf']['field_5e886a3273989'];
update_user_meta($user_id, 'first_name', $_POST['acf']['field_5e880c9b0e92d'] );
update_user_meta($user_id, 'last_name', $_POST['acf']['field_5e880d0daa23b'] );
update_user_meta($user_id, 'nickname', $_POST['acf']['field_5e880d3baa23c'] );
update_user_meta($user_id, 'display_name', $_POST['acf']['field_5e886a3273989'] );
}
return $post_id;
}
add_action('acf/save_post', 'update_member_data', 20);
Note: wp_update_user does not handle meta updates, you need to use update_user_meta.
Graeme
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.