Hello,
I am struggling a bit with a user data update form in my font-end.
– The profile fields group fields shows in my back-end user profile
– I cannot get my wp_update_user( $args ); to actually update the user meta, although the acf fields update fine.
.. I think my function is not hooking to the form ..
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(26734, $avatar_group),
'submit_value' => 'Update Profile');
?>
<?php acf_form( $options );?>
.. and my function in a global network activated (multisite) plugin:
/** 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'];
$memberFirstname = $_POST['acf']['field_5e880c9b0e92d'];
$memberLastname = $_POST['acf']['field_5e880d0daa23b'];
$memberNickname = $_POST['acf']['field_5e880d3baa23c'];
$user_id = str_replace("user_", "", $post_id);
if (isset($memberEmail)) {
if (email_exists( $memberEmail )){
update_field('field_5e880d5caa23d', get_the_author_meta('user_email',$user_id), $post_id);
} else {
$args = array(
'ID' => $user_id,
'first_name' => esc_attr( $memberFirstname ),
'last_name' => esc_attr( $memberLastname ),
'nickname' => esc_attr( $memberNickname ),
'user_email' => esc_attr( $memberEmail )
);
wp_update_user( $args );
}
}
}
return $post_id;
}
add_action('acf/save_post', 'update_member_data', 20);
Help would be appreciated.
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
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.