Home › Forums › Front-end Issues › Don't update email if it exists. Show error on same page in › Reply To: Don't update email if it exists. Show error on same page in
My issue is as follows. I am creating a shortcode which outputs the ACF form with a custom field group. Some of those fields will update the corresponding user fields. I want to have users update their profiles using ACF and custom fields like apartment number and phone(s)
1. First problem is that the ACF form outputted by the shortcode is going outside of the WYSIWYG editor and the container. You can see that in Screenshot A. B. and C.
2. My other problem is that I want the email to not update if it already exists. So if the user enters an email already taken by another user I’d like it to show an error at the top of the page and not submit any values. Actually it would be even better if it showed as an error. See Screenshot G. But what happens is it posts and then the error is just on a page with nothing else. See screenshot D. The email is also updated anyway – see screenshot E. Although the ACF is updated but not the WordPress user which makes sense because it must be unique. See screenshot F.
Screenshots
ACF form generated from field group. Email, first name, lastname, meant to update the corresponding user fields.
https://zorahosting.com/user form.png
Here is the container showing a custom class ‘form-update-container’. This is the container of the shortcode
A. https://zorahosting.com/form-update-container.png
This is the shortcode in the WYSIWYG WordPress editor
B. https://zorahosting.com/userupdate-shortcode.png
Shortcode is not showing up in the container but outside of it
C. https://zorahosting.com/outside-of-container.png
Error: ‘the email already exists’ shows up on a new page. I’d like the error to show up somewhere else. either at top or bottom of form. In the #message part above the form.
D. https://zorahosting.com/email-exists-in-window.png
Email is updated even though it exists. Although the ACF email is allowed to update. It has not updated the user because WordPress won’t allow to of the same email.
E. https://zorahosting.com/email-updated-anyway.png
ACF email updated not user from email that existed on form submit
F. https://zorahosting.com/acf-updated-not-user.png
Red error box generated by regular expression ( this is not an email )
G. http://zorahosting.com/red-error-box.png
Here below is my code
The shortcode outputting the form:
– – – – – – – – – – – – – – – – – – – – – – –
function zUpdate( $atts, $content = null ) {
$current_id = get_current_user_id();
$user_id = 'user_' . $current_id;
$options = array(
'post_id' => $user_id,
/* my field group grabs unit, home phone, and cell phone acf */
'field_groups' => array('3307', ),
'submit_value' => 'Update'
);
$form = acf_form( $options );
return $form;
}
The shortcode updating the form with ACF data updating the WP user data
function my_acf_save_post( $post_id ) {
if( empty($_POST['acf']) ) {
return;
}
if( is_admin() ) {
return;
}
//get current email before change
$current_email = get_the_author_meta('user_email', $wp_user_id);
if( $_POST['post_id'] != 'new' ) {
$emailField = $_POST['acf']['field_5ae7a967009de'];
$wp_user_id = str_replace("user_", "", $post_id);
if (isset($emailField)) {
if (email_exists( $emailField )) {
echo "<p>Email already exists</p>";
update_field('field_5ae7a967009de', $current_email, $post_id);
} else {
$args = array(
'ID' => $wp_user_id,
'user_email' => esc_attr( $emailField )
);
wp_update_user( $args );
}
}
$first_name = $_POST['acf']['field_5ae7a4b8e5948'];
$last_name = $_POST['acf']['field_5ae7a8cf8e3e0'];
$user_data = wp_update_user( array( 'ID' => $wp_user_id, 'first_name' => $first_name, 'last_name' => $last_name ) );
}
return $post_id;
}
both are called at the end of course:
– – – – – – – – – – – – – – – – – – – – – – –
add_action('acf/save_post', 'my_acf_save_post', 1);
add_shortcode('userupdate', 'zUpdate');
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.