Support

Account

Home Forums Front-end Issues acf/save_post hook how to display error message

Solving

acf/save_post hook how to display error message

  • I am using acf_form function. Form has been build up with ACF in Backend. Please check screenshot. With using form, one can create their account in my website.

    Below is my code :

    acf_form([
            'id'            => 'acf-register-form',
            'field_groups'  => [ 56 ],
            'post_id'       => 'create_user',
            'submit_value' => __("Sign Up", 'acf'),
            'updated_message' => __("Registered Successfully", 'acf'),
        ]);

    In above function, “56” is my field group ID which i had created in Admin panel.

    Now i am using acf/save_post hook by which user can create their own account. And i am using wp_insert_user() function for new account creation.

    Below you can find acf/save_post hook.

    add_action( 'acf/save_post', 'cclr_create_new_user', 10,1 );
    
    function cclr_create_new_user( $post_id ) {
    	
    	if ( empty($_POST['acf']) || 
    		empty($_POST['acf']['field_5e4282dbd4ddd']) || 
    		empty($_POST['acf']['field_5e4284f25b26f']) || 
    		empty($_POST['acf']['field_5e42850b5b270']) || 
    		false === strpos($post_id, 'user') ) { 
    			return false;
    	}
    
    	// Create a new user
    	if ( 'create_user' === $post_id )
    	{
    		$user_name 		= sanitize_text_field($_POST['acf']['field_5e42850b5b270']);
    		$user_email 	= sanitize_email($_POST['acf']['field_5e4282dbd4ddd']);
    		$user_password 	= sanitize_text_field($_POST['acf']['field_5e4284f25b26f']);
    
    		$new_user_id 	= wp_insert_user(array(
    							            'first_name' => $user_name,
    							            'user_email' => $user_email,
    							            'user_login' => $user_email,
    							            'user_pass'  => $user_password,
    							            'role'       => 'subscriber'
    							        ));
        	if( is_wp_error( $new_user_id ) )
        	{
        		$error_string = $result->get_error_message();
       			echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
        	}
        	else
        		return true;
    	}
    	else
    		return false;
    }

    Issue 1 :
    Now in above code if any error comes from wp_insert_user, then it is not showing error in front end form. Each and every time it give me “Registered Successfully” message.

    Issue 2 :
    In addition to this, i also found that those record is also added in wp_options table. How to prevent inserting data in wp_options table. Please check attached screenshot.

    Thanks in advance.

  • In the above code, some little change

    if( is_wp_error( $new_user_id ) )
        	{
        		$error_string = $new_user_id->get_error_message();
       			echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
        	}

    But still error message is not being displayed in Front End Registration Form.

  • For the first issue, you cannot report errors on the acf/save_post hook. In order to do error checking before the new values are inserted you need to create acf/validate_value filters for your fields. https://www.advancedcustomfields.com/resources/acf-validate_value/ and test the values to ensure an error will not happen before you attempt to insert the user.

    For the second problem, inserting the values into options. The reason for this is that the post ID value is, when ACF saves the post it does not know what “create_user” means and assumes it is options. You should not be using an acf/save_post filter in case. What you should be using is an acf/pre_save_post filter https://www.advancedcustomfields.com/resources/acf-pre_save_post/

  • Hi John,

    Thanks for your quick reply.

    For Second problem, regarding data is being saved under wp_options table, now i am using acf/pre_save_post filter instead of acf/save_post filter. Now it’s work properly. Now no more data is being saved in wp_options table.

    For First issue, i still have some confusion, where do i write below code, i meant to say in which hook do i write code ?

    // Create a new user
    	if ( 'create_user' === $post_id )
    	{
    		$user_name 		= sanitize_text_field($_POST['acf']['field_5e42850b5b270']);
    		$user_email 	= sanitize_email($_POST['acf']['field_5e4282dbd4ddd']);
    		$user_password 	= sanitize_text_field($_POST['acf']['field_5e4284f25b26f']);
    
    		$new_user_id 	= wp_insert_user(array(
    							            'first_name' => $user_name,
    							            'user_email' => $user_email,
    							            'user_login' => $user_email,
    							            'user_pass'  => $user_password,
    							            'role'       => 'subscriber'
    							        ));
        	if( is_wp_error( $new_user_id ) )
        	{
        		$error_string = $new_user_id->get_error_message();
       			echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
        	}
        	else
        		return true;

    Because as you mentioned that to validate field, we need to use acf/validate_value filter for all fields. Because we don’t know at what circumstances wp_insert_user() function will return error.
    So my question is that when using acf/pre_save_post filter, if any error occur runtime inside wp_insert_user() function, then how can we show error message ?

  • You would still insert the user in the acf/pre_save_post filter.

    You need to look at the reasons that inserting a user may return an error, for example, an email address that is already used will cause an error, so you need to check for this condition. To be honest, I don’t know all of the things that can cause an error, but you need to figure them out and make sure that the errors cannot happen.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

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.