Written by Giles Bennett
A number of times have cropped up where I've got FTP or SFTP access to a WP installation, but no username or password details. Rather than wait for the client to reply, it's often easier to just create a new user in Wordpress programmatically. There are a number of different methods of doing this, and I'll deal with them in a sporadic series of posts.
The easiest method (in my view) involves editing the theme's functions.php file to add a small one-off function that creates the user, then makes them an admin.
To use this method, simply download your theme's functions.php file, and create a backup copy of it (you'll need it later).
Then insert the script below into the very bottom of it, before the closing ?> php tag. Be sure to replace the $username, $password and $email variables with your chosen values.
add_action('init', 'create_a_user');
function create_a_user() {
$username = 'username';
$password = 'password';
$email = 'your@emailaddress.com';
$user_id = wp_create_user( $username, $password, $email );
$user = get_user_by( 'id', $user_id );
$user->remove_role( 'subscriber' );
$user->add_role( 'administrator' );
}
Upload it in place of the existing functions.php, then visit any page on your Wordpress installation - it doesn't matter which one. Then - and this is important - remove that code from your functions.php as otherwise Wordpress'll try to create the new user every time someone visits a page, and it'll fall over (because the user has already been created). Then log in using the details that you put in the script, and you're done!