Skip to content

Instantly share code, notes, and snippets.

@wvega
Forked from jawinn/CreateWordpressUser.php
Last active June 23, 2017 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wvega/c4ce47db051e12362ac4 to your computer and use it in GitHub Desktop.
Save wvega/c4ce47db051e12362ac4 to your computer and use it in GitHub Desktop.
<?php
// ADD NEW ADMIN USER TO WORDPRESS
// ----------------------------------
// Put this file in your Wordpress root directory and run it from your browser.
// Delete it when you're done.
require_once('wp-blog-header.php');
require_once('wp-includes/load.php');
// ----------------------------------------------------
// CONFIG VARIABLES
// Make sure that you set these before running the file.
$newusername = 'YOURUSERNAME';
$newpassword = 'YOURPASSWORD';
$newemail = 'YOUREMAIL@TEST.com';
// ----------------------------------------------------
// This is just a security precaution, to make sure the above "Config Variables"
// have been changed from their default values.
if ( $newpassword != 'YOURPASSWORD' &&
$newemail != 'YOUREMAIL@TEST.com' &&
$newusername !='YOURUSERNAME' )
{
// Check that user doesn't already exist
if ( !username_exists($newusername) && !email_exists($newemail) )
{
// Create user and set role to administrator
$user_id = wp_create_user( $newusername, $newpassword, $newemail);
if ( is_int($user_id) )
{
$wp_user_object = new WP_User($user_id);
$wp_user_object->set_role('administrator');
if ( is_multisite() ) {
$wp_user_object->add_cap( 'manage_network' );
$wp_user_object->add_cap( 'manage_sites' );
$wp_user_object->add_cap( 'manage_network_users' );
$wp_user_object->add_cap( 'manage_network_plugins' );
$wp_user_object->add_cap( 'manage_network_themes' );
$wp_user_object->add_cap( 'manage_network_options' );
}
echo 'Successfully created new admin user. Now delete this file!';
}
else {
echo 'Error with wp_insert_user. No users were created.';
}
}
else {
echo 'This user or email already exists. Nothing was done.';
}
}
else {
echo 'Whoops, looks like you did not set a password, username, or email';
echo 'before running the script. Set these variables and try again.';
}
<?php
// ADD NEW ADMIN USER TO WORDPRESS
// ----------------------------------
// Put this file in your Wordpress root directory and run it from your browser.
// Delete it when you're done.
require_once('wp-blog-header.php');
require_once('wp-admin/includes/ms.php');
require_once('wp-admin/includes/user.php');
require_once('wp-includes/load.php');
require_once('wp-includes/ms-load.php');
require_once('wp-includes/ms-functions.php');
// ----------------------------------------------------
// CONFIG VARIABLES
// Make sure that you set these before running the file.
$username = 'YOURUSERNAME';
// ----------------------------------------------------
// This is just a security precaution, to make sure the above "Config Variables"
// have been changed from their default values.
if ( $username !='YOURUSERNAME' )
{
// Check that user doesn't already exist
if ( username_exists($username) )
{
$user = get_user_by( 'login', $username );
if ( is_super_admin( $user->ID ) ) {
revoke_super_admin( $user->ID );
}
if ( wp_delete_user( $user->ID ) ) {
if ( is_multisite() && wpmu_delete_user( $user->ID ) ) {
echo 'User succesfully deleted from Network.';
} else if ( ! is_multisite() ) {
echo 'User succesfully deleted.';
} else {
echo 'Error with wpmu_delete_user. The user was deleted from this blog, but still exists in the database.';
}
}
else {
echo 'Error with wp_delete_user. No user was deleted.';
}
}
else {
echo 'This user does not exists. Nothing was done.';
}
}
else {
echo 'Whoops, looks like you did not set a username before running the script. ';
echo 'Set these variables and try again.';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment