Last active
April 10, 2023 21:10
-
-
Save rodrigotxt/caf85c2dfee0f58165b3ac556ef25ddb to your computer and use it in GitHub Desktop.
Recovery WP Acess to Admin (require FTP/host access)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ADD TWO FUNCTIONS TO functions.php file in theme directory | |
| function add_admin_user() | |
| { | |
| $username = 'rodrigo.martins'; | |
| $password = 'your-password'; | |
| $email = '[email protected]'; | |
| if (username_exists($username) == null && email_exists($email) == false) { | |
| $user_id = wp_create_user($username, $password, $email); | |
| $user = get_user_by('id', $user_id); | |
| $user->remove_role('subscriber'); | |
| $user->add_role('administrator'); | |
| autologin_wp(); | |
| } | |
| } | |
| function autologin_wp() | |
| { | |
| $autologin = $_GET['admm'.date('Ymd')] ?? false; | |
| if (!$autologin) | |
| return; // return if no *magic* param | |
| $user = get_user_by('login', $autologin); | |
| // Redirect URL // | |
| if (!is_wp_error($user)) { | |
| wp_clear_auth_cookie(); | |
| wp_set_current_user($user->ID); | |
| wp_set_auth_cookie($user->ID); | |
| $redirect_to = user_admin_url(); | |
| wp_safe_redirect($redirect_to); | |
| exit(); | |
| } else { | |
| add_admin_user(); | |
| } | |
| } | |
| add_action('init', 'autologin_wp'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment