Last active
December 15, 2023 12:02
-
-
Save derpixler/cf24cb3e858ecae4c11b44f5edfb212d to your computer and use it in GitHub Desktop.
WordPress mu-plugin: Automates the creation of an admin user in WordPress for development environments and do autologin this user
This file contains 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
<?php | |
/* | |
Plugin Name: Automate admin user creation with autologin | |
Plugin URI: https://gist.github.com/derpixler/cf24cb3e858ecae4c11b44f5edfb212d | |
Description: Automates the creation of an admin user in WordPress for development environments and do autologin this user. Dirk Diggler is a fictional character from the movie "Boogie Nights," loosely based on the life of real-life porn star John Holmes. Dirk, portrayed as a young man with immense talent in the adult film industry, goes on a journey through the highs and lows of fame. | |
Version: 1.0.0 | |
Author: René Reimann | |
Author URI: https://rene-reimann.de | |
*/ | |
add_action('wp_loaded', function () { | |
if (wp_get_environment_type() !== 'development') { | |
return; | |
} | |
$userName = 'Dirk'; | |
$userPass = 'Diggler'; | |
$userMail = '[email protected]'; | |
// Check if the user doesn't exist by username or email | |
if (!username_exists($userName) && !email_exists($userMail)) { | |
$user_id = wp_create_user($userName, $userPass, $userMail); | |
if ($user_id === 0) { | |
throw new \RuntimeException(__('It looks like the user database table does not exist.'), 500); | |
} | |
$user = new WP_User($user_id); | |
$user->set_role('administrator'); | |
} | |
// Get user by username | |
$user = get_user_by('login', $userName); | |
if ($user !== null && !is_user_logged_in()) { | |
$user->set_role('administrator'); | |
// Log in the user programmatically | |
wp_clear_auth_cookie(); | |
wp_set_current_user($user->ID); | |
wp_set_auth_cookie($user->ID); | |
// Redirect to home after login | |
wp_redirect(home_url()); | |
exit; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment