Support

Account

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

Solved

wp_insert_user on save

  • Hi,
    The ACF plugin has been essential to the development of my sites so far, with no issues whatsoever. I have just come across an odd issue though and hope that someone can help. It’s most likely to be a quick fix with the coding rather than an issue with ACF itself.

    I have a custom post type set up ‘homeowner’, which has it’s own set of ACF custom fields attached. I have a function ‘homeowner_postdata’ in functions.php attached to add_filter(‘publish_homeowner’, ‘homeowner_postdata’). When a homeowner is published they are given a user account on the site, so in the ‘homeowner_postdata’ function I have a wp_insert_user inserting a new user with a generated password, login and email address.

    The problem is, each time wp_insert_user fires, it is passing the custom fields from Homeowner to the new user, together with the password, username and email address. So, the Homeowner custom fields are being saved to both wp_postmeta and wp_usermeta tables.

    The code for the filter is:

    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'
            );
    	$status = wp_insert_user( $userargs );  
            if ( is_wp_error($status) )  {
                //echo "User not created.";  
            } else { 
                //echo "User created.";  
            }
         }
    }

    I would like to keep the Homeowner data in wp_postmeta and the basic user profile in wp_usermeta. Any help would be appreciated.

  • Hi @lemonaise

    Thanks for the bug report.

    It seems that because the $_POST['acf_nonce'] is set when saving the post and inserting the user, ACF is saving the data for both of them.

    I will add this to the to-do and have it fixed soon.

    For now, I would play around with unsetting the $_POST['acf_nonce'] varaible BEFORE you insert the user.

    Hope that works for you.

    Thanks
    E

  • Thanks Elliot,
    Unsetting $_POST[‘acf_nonce’] before inserting the user stops the ACF fields from being passed to the new user account. That works great, but the publish hook fires before the post is saved, meaning the ACF fields are not saved to the custom post record.

    Do you know if there’s a way to change the order of the publish function to fire after the post save? I’ll of course experiment with this too and post back here if I find a solution.

    Thanks again

  • 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.";  
            }
         }
    }
  • Has $_POST['acf_nonce'] been fixed, Elliot?

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

The topic ‘wp_insert_user on save’ is closed to new replies.