• Resolved Erfan MHDi

    (@erfanmhd)


    hello,

    im using these line of codes to create user programmatically but it seems like i cannot change the user status to pending or un approved!

    
    $NewUser = wp_insert_user( 
    	array(
    		'user_login'   => $UserName,
    		'user_pass'    => wp_generate_password( 10, true, true ),
    		'user_email'   => $UserEmail,
    		'role'         => 'test',
    	)
    );
    
    update_user_meta( $NewUser, 'currency', $UserItem['custName'] );
    update_user_meta( $NewUser, 'wppb_approval_status', 'unapproved' );

    i appreciate it if you guys can give me a hint on how can i create user and set the status on pending or unapproved ?!

    Thanks in advance

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello @erfanmhd,

    Thank you for reaching out to us.

    My suggestion would be to use this:

    wppb_update_user_status_on_admin_registration( $NewUser );

    instead of this:

    update_user_meta( $NewUser, ‘wppb_approval_status’, ‘unapproved’ );

    It will automatically put your new users on pending.

    Kind regards,

    Thread Starter Erfan MHDi

    (@erfanmhd)

    hello,

    Thank you for your reply,
    i’ve tried this

    wppb_update_user_status_on_admin_registration( $NewUser );

    but the users are still being created with Approved User Status.

    here is the main code i’m using to import users from an external api using ACF Option Page.

    function API_Bulk_User_Importer() {
    	
        $screen = get_current_screen();
        if (strpos($screen->id, 'user-importer') == true) {
    	    		
    		$NewUser = wp_insert_user( 
    			array(
    				'user_login'   => $UserName, //from api
    				'user_pass'    => wp_generate_password( 10, true, true ),
    				'user_email'   => $UserEmail,//from api
    				'role'         => 'business_account',
    			)
    		);
    
    		if ( is_wp_error($NewUser) ) {
    		  //handle error here
    		} else {
    			$user = get_user_by('id', $NewUser);
    			update_user_meta( $NewUser, 'company', $UserItem['company'] );
    			wppb_update_user_status_on_admin_registration( $NewUser );
    
    			
    		  //handle successful creation here
    		}
    		    
      
      
        }
    }
    add_action('acf/save_post', 'API_Bulk_User_Importer', 10);

    Thanks in Advance.

    Hello @erfanmhd,

    Try adding this code directly into that function:

    wp_set_object_terms($NewUser, apply_filters( 'wppb_admin_approval_update_user_status', array('pending'), $NewUser ), 'user_status', false);
                clean_object_term_cache($NewUser, 'user_status');
                add_user_meta( $NewUser, '_wppb_admin_approval_link_param', wppb_get_admin_approval_email_link_key($NewUser) );;
                do_action('wppb_new_user_pending_approval', $NewUser );

    Does it work now?

    Kind regards,

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘how to create user programmatically with unapproved status’ is closed to new replies.