I use the following code in a frontend form. About 10% of the time, the new user is created as a 'subscriber'. What would cause this erratic behavior?

$new_user_id = wp_insert_user(array(
  'role' => 'customer',
  'user_login' => $user_login,
  'user_pass' => $user_pass,
  'user_email' => sanitize_text_field($user_email),
  'first_name' => sanitize_text_field($user_first),
  'last_name' => sanitize_text_field($user_last),
  'user_registered' => date('Y-m-d H:i:s'),
) );

I have also tried update_user_meta( $new_user_id, 'role', 'customer' ) after the wp_insert_user to catch those that have slipped through, but that does not seem to work.

  • 1
    Our of curiosity, when you add the customer role, do you add it once like wp_insert_post? Or do you add it on every single page load like register_post_type? – Tom J Nowell Nov 12 '19 at 22:33
  • I register the role like so: function add_roles_on_plugin_activation() { add_role( 'customer',( 'Customer' ), array( 'read' => true ) ); } register_activation_hook( __FILE, 'add_roles_on_plugin_activation' ); – Michele Troxler Nov 14 '19 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.