How to create WordPress user programmatically

  • ‘ID’
    (int) User ID. If supplied, the user will be updated.
  • ‘user_pass’
    (string) The plain-text user password.
  • ‘user_login’
    (string) The user’s login username.
  • ‘user_nicename’
    (string) The URL-friendly user name.
  • ‘user_url’
    (string) The user URL.
  • ‘user_email’
    (string) The user email address.
  • ‘display_name’
    (string) The user’s display name. Default is the user’s username.
  • ‘nickname’
    (string) The user’s nickname. Default is the user’s username.
  • ‘first_name’
    (string) The user’s first name. For new users, will be used to build the first part of the user’s display name if is not specified.
  • ‘last_name’
    (string) The user’s last name. For new users, will be used to build the second part of the user’s display name if is not specified.
  • ‘description’
    (string) The user’s biographical description.
  • ‘rich_editing’
    (string) Whether to enable the rich-editor for the user. Accepts ‘true’ or ‘false’ as a string literal, not boolean. Default ‘true’.
  • ‘syntax_highlighting’
    (string) Whether to enable the rich code editor for the user. Accepts ‘true’ or ‘false’ as a string literal, not boolean. Default ‘true’.
  • ‘comment_shortcuts’
    (string) Whether to enable comment moderation keyboard shortcuts for the user. Accepts ‘true’ or ‘false’ as a string literal, not boolean. Default ‘false’.
  • ‘admin_color’
    (string) Admin color scheme for the user. Default ‘fresh’.
  • ‘use_ssl’
    (bool) Whether the user should always access the admin over https. Default false.
  • ‘user_registered’
    (string) Date the user registered in UTC. Format is ‘Y-m-d H:i:s’.
  • ‘user_activation_key’
    (string) Password reset key. Default empty.
  • ‘spam’
    (bool) Multisite only. Whether the user is marked as spam. Default false.
  • ‘show_admin_bar_front’
    (string) Whether to display the Admin Bar for the user on the site’s front end. Accepts ‘true’ or ‘false’ as a string literal, not boolean. Default ‘true’.
  • ‘role’
    (string) User’s role.
  • ‘locale’
    (string) User’s locale. Default empty.
  • ‘meta_input’
    (array) Array of custom user meta values keyed by meta key. Default empty.

Example

add_action(‘admin_init’,’create_user_meta’);
function create_user_meta(){
wp_insert_user( array(
‘user_login’ => ‘janedoe’,
‘user_pass’ => ‘passwordhere’,
‘user_email’ => ‘jane.doe@example.com’,
‘first_name’ => ‘Jane’,
‘last_name’ => ‘Doe’,
‘display_name’ => ‘Jane Doe’,
‘role’ => ‘editor’
));
}

Check if the user exists

add_action(‘admin_init’,’if_user_exists’);
function if_user_exists(){
if( null == username_exists( ‘janedoe’ ) ) {
echo “not an Existing User”;// Do something for unexistent user
}
else{
echo “Existing User”; // Do something if user name exists
}
}

Leave a Comment

Your email address will not be published. Required fields are marked *