Support

Account

Home Forums Backend Issues (wp-admin) wp_insert_user on save Reply To: wp_insert_user on save

  • I had no luck changing the order, but I’ve fixed this by just unsetting the post data, then adding it back on after the user has been created. Here’s the complete function, in case it’s of use to someone else.

    add_filter('publish_homeowner', 'homeowner_postdata');
    function homeowner_postdata( $post_id ) {
    	global $wpdb;
    	$username = preg_replace("/[^A-Za-z0-9]/", "", strtolower(get_the_title($post_id)));
    	$email = preg_replace("/[^A-Za-z0-9]/", "", strtolower(str_replace(" ", ".", get_the_title($post_id))));
            $random_password = wp_generate_password( 8, false );
            $userargs = array(
                 'user_pass' => $random_password,
            	'user_login' => $username,
            	'user_email' => $email,
            	'role' => 'homeowner'
            );
            $acf_nonce = $_POST['acf_nonce'];
            $_POST['acf_nonce'] = '';
    	$status = wp_insert_user( $userargs );  
            if ( is_wp_error($status) )  {
                //echo "User not created.";  
            } else {
                $_POST['acf_nonce'] = $acf_nonce;
                //echo "User created.";  
            }
         }
    }