Skip to content

Instantly share code, notes, and snippets.

@shawn-crigger
Created October 26, 2018 15:17
Show Gist options
  • Save shawn-crigger/cb949a26739d35572904c162c75ce27e to your computer and use it in GitHub Desktop.
Save shawn-crigger/cb949a26739d35572904c162c75ce27e to your computer and use it in GitHub Desktop.
Adds new user to wp, created like this because user table was broken and wp_insertuser was not working due to duplicate primary id equal 0 error
<?php
if ( isset( $_GET['test'] ) && 'shawn' == $_GET['test'] ) :
add_action('init', 'make_user');
endif;
function make_user() {
global $wpdb;
$wpdb->show_errors();
$password = wp_generate_password(12, false);
echo 'New password: '.$password.'<br>';
/*
// uncomment this if you need to see what the last user id is
$sql = "SELECT id, user_login, user_email FROM wp_users";
$res = $wpdb->get_results( $sql );
dump($res);
wp_die();
*/
// change this id to the next unused one
$user_id = 5;
$userdata = array(
'ID' => $user_id,
'user_login' => 'marcia',
'user_pass' => $password,
'user_nicename' => 'marcia',
'user_email' => 'marcia@vacationrentalsofnmb.com',
'user_status' => '1',
// 'role' => 'administrator',
);
$id = $wpdb->insert( 'wp_users', $userdata );
$u = new WP_User( $user_id );
// Add role
$u->add_role('administrator');
wp_set_password( $password, $user_id );
$d = get_userdata( $user_id );
dump($d);
wp_die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment