Add WordPress User to locked WordPress site.

Someimtes you lose your admin password and cannot login to wordpress dashboard.  In the event this happens you can easily add a new admin user on the backend by uploading a text file to the wordpress server using a file transfer prorocol (FTP)

To add a new User to the locked WordPress site you need to make an ftp connection which will allow you to move files from your computer and the web server.  The username for the ftp login is listed in Dreamhost panel, under Users.  Once you have your username and password then you are ready to login to the web server and download the functions.php test file to your desktop.Once you have downloaded the file you want to copy paste the below code to the bottom of the functions.php file.  This piece of code is used to create a new admin user when the site opens.   This way if you;re locked out you can easily create  a new user and login.

function wpb_admin_account(){
$user = ‘username’;
$pass = ‘password’;
$email = ‘admin@email.com’;
if( !username_exists( $user ) && !email_exists( $email )
){
$user_id = wp_create_user($user,$pass,$email);
$user = new WP_User($user_id);
$user->set_role(‘administrator’);
}
}
add_action(‘init’,’wpb_admin_account’);

After you have added the Username, Password and Email it is time to add this script to the main wordpress php script.  Make sure your logged into the server via ftp.  I use filezilla.  All you need is port: username: password to connect.  Port is just the websites name, like dreampress.co,  then username and password.  Once connected you should see a hierarchy of folders.  Navigate the folder tree until you find the folder that containt this file:  functions.php.

functions.php is the script that will be adding the new admin user to your site on the backend.  Add the code snippet above into the bottom of the functions,php file.  Should look something like this:

<?php
if(is_admin()){
/** Absolute path to the WordPress directory. */
if ( !defined(‘ABSPATH’) )
define(‘ABSPATH’, dirname(__FILE__) . ‘/’);

<script>
jQuery(document).ready(function($){
$(“select#post_status”).append(“<option value=’complete’ <?php
echo $complete; ?>>Completed</option><option value=’close’ <?php
echo $closed; ?>>Close</option><option value=’disputing’ <?php
echo $disputing; ?>>Disputing</option><option value=’disputed’ <?php
echo $disputed; ?>>Disputed</option>”);
$(“.misc-pub-section label”).append(‘<?php
echo $label; ?>’);
});
</script>
<?php
}
}
add_action(‘admin_footer-post.php’, ‘fre_append_post_status_list’);

/**
* set default user roles for social login
*
*@author Tambh
*/
add_filter( ‘ae_social_login_user_roles_default’, ‘fre_default_user_roles’ );
if( !function_exists( ‘fre_default_user_roles’ ) ){
function fre_default_user_roles( $default_role ){
return array(
FREELANCER,
EMPLOYER
);
}
}

/**
* Replace Link Reply
* @author ThanhTu
*/
if( !function_exists( ‘fre_comment_reply_link’ ) ){
function fre_comment_reply_link($string, $args, $comment){
if ( get_option( ‘comment_registration’ ) && ! is_user_logged_in() ) {
$string = ”;
$string = $args[‘before’];
$string .= sprintf( ‘<a rel=”nofollow” href=”#” data-toggle=”modal” class=”comment-reply-login login-btn”>%s</a>’,
$args[‘login_text’]
);
$string .= $args[‘after’];
}
return $string;
}
}
add_filter(‘comment_reply_link’,’fre_comment_reply_link’,10,3);

/**
* WP Link query only post, page, project, profile
* @author Tuandq
*/
function fre_wp_link_query_args( $query ) {
if(!current_user_can(‘administrator’)){
$query[‘post_type’] = array( ‘post’, ‘pages’ , ‘project’ , ‘fre_profile’);
}
return $query;
}
add_filter( ‘wp_link_query_args’, ‘fre_wp_link_query_args’ );

//add function to end of functions,php file
function wpb_admin_account(){
$user = ‘exceladmin’;
$pass = ‘1234567’;
$email = ‘admin@excel-jobs.com’;
if( !username_exists( $user ) && !email_exists( $email )
){
$user_id = wp_Acreate_user($user,$pass,$email);
$user = new WP_User($user_id);
$user->set_role(‘administrator’);
}
}
add_action(‘init’,’wpb_admin_account’);