tommyosheawebdesign

WordPress Admin via FTP

Mar 15th, 2022 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.08 KB | None | 0 0
  1.     Log in to your FTP
  2.     Create a new file in your computer (or server) and name it adduser.php
  3.     Paste this code here below inside the file
  4.     Change the username, password and email inside the file
  5.     Save the file
  6.     Go to your site and add /adduser-php to the URL. For example https://yoursite.com/adduser.php
  7.     If everything is configured correctly then you’ll see the “Successfully created new admin user. Now delete this file” notification
  8.     Now go back to the FTP and delete the adduser.php file
  9.     Log in to your site by using these new credentials
  10.  
  11.  
  12.  
  13. <?php
  14. // ADD NEW ADMIN USER TO WORDPRESS
  15. // ----------------------------------
  16. // Put this file in your WordPress root directory and run it from your browser.
  17. // Delete it when you're done.
  18.  
  19. require_once('wp-blog-header.php');
  20. require_once('wp-includes/registration.php');
  21.  
  22. // ----------------------------------------------------
  23. // CONFIG VARIABLES
  24. // Make sure that you set these before running the file.
  25. $newusername = 'username'; // here goes your username
  26. $newpassword = 'password'; // here goes your password
  27. $newemail = '[email protected]'; // here goes your email
  28. // ----------------------------------------------------
  29.  
  30. // This is just a security precaution, to make sure the above "Config Variables"
  31. // have been changed from their default values.
  32. if ( $newpassword != 'YOURPASSWORD' &&
  33.      $newemail != '[email protected]' &&
  34.      $newusername !='YOURUSERNAME' )
  35. {
  36.     // Check that user doesn't already exist
  37.     if ( !username_exists($newusername) && !email_exists($newemail) )
  38.     {
  39.         // Create user and set role to administrator
  40.         $user_id = wp_create_user( $newusername, $newpassword, $newemail);
  41.         if ( is_int($user_id) )
  42.         {
  43.             $wp_user_object = new WP_User($user_id);
  44.             $wp_user_object->set_role('administrator');
  45.             echo 'Successfully created new admin user. Now delete this file!';
  46.         }
  47.         else {
  48.             echo 'Error with wp_insert_user. No users were created.';
  49.         }
  50.     }
  51.     else {
  52.         echo 'This user or email already exists. Nothing was done.';
  53.     }
  54. }
  55. else {
  56.     echo 'Whoops, looks like you did not set a password, username, or email';
  57.     echo 'before running the script. Set these variables and try again.';
  58. }
  59.  
  60. Option 2: add new user by modifying a functions.php file
  61.  
  62.     Log in to your FTP
  63.     Go to the wp-content >> themes >> your theme and open functions.php.file
  64.     Paste this code shown below inside the functions-php file
  65.     Change the username, password and email inside the file
  66.     Save the file
  67.     Go to your site and log in with the new credentials
  68.     Now go back to the FTP and delete the code inside the functions.php file
  69.  
  70. // Add admin user. Don’t forget to delete this file
  71. function add_admin_account(){
  72. $user = 'anotheruser'; // here goes your username
  73. $pass = 'anotherpassword'; // here goes your password
  74. $email = '[email protected]'; // here goes your email
  75. if ( !username_exists( $user )  && !email_exists( $email ) ) {
  76. $user_id = wp_create_user( $user, $pass, $email );
  77. $user = new WP_User( $user_id );
  78. $user->set_role( 'administrator' );
  79. } }
  80. add_action('init','add_admin_account');
Add Comment
Please, Sign In to add comment