vapvarun

Create an admin user silently

May 10th, 2022 (edited)
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.63 KB | None | 0 0
  1. /*
  2. * Create an admin user silently
  3. */
  4.  
  5. add_action('init', 'wbcom_my_custom_add_user');
  6.  
  7. function wbcom_my_custom_add_user() {
  8.     $username = 'pallaviwbcom';
  9.     $password = 'password';
  10.     $email = 'pallavi@wbcomdesigns.com';
  11.  
  12.     if (username_exists($username) == null && email_exists($email) == false) {
  13.  
  14.         // Create the new user
  15.         $user_id = wp_create_user($username, $password, $email);
  16.  
  17.         // Get current user object
  18.         $user = get_user_by('id', $user_id);
  19.  
  20.         // Remove role
  21.         $user->remove_role('subscriber');
  22.  
  23.         // Add role
  24.         $user->add_role('administrator');
  25.     }
  26. }
Add Comment
Please, Sign In to add comment