Hi, I think I'm not so far but I can't figure it out...
I have created a CPT named "Client" with an email field.
When I add a new client, I save it and automatically it creates a WP user with his email address. I know how to create it, and I know how to check if email already exist in WP users.
What I'm not able to do is :
- Do NOT save the post if email already exist
- Go back to post and display an error notice in that case saying "Email already exist"
Here is my code :
function my_add_user_client($post_id)
{
$email = rwmb_get_value( 'email_contact', $post_id );
$password = rwmb_get_value( 'password_contact', $post_id );
$verif_mail = email_exists( $email );
if($verif_mail){
//Don't save, go back and display a notice with an error
}else{
$user_id = wp_create_user( $email, $password, $email );
$user_id_role = new WP_User($user_id);
$user_id_role->set_role('contributor');
}
}
add_action('save_post_client', 'my_add_user_client', 20 );
Thanks for your help !