How I add an admin in WordPress in functions.php without being an admin

You can add an admin user in WordPress without being an admin. All you need to do is to add the below PHP code inside your theme’s functions.php file.

Firstly,  you need to insert the code inside the functions.php file found in  your website themes folder. You can upload the code via FTP or by accessing the Web Server directly via Cpanel.

The file functions.php is located in the following path: yourwebsite.com/wp-content/themes/your-theme/functions.php

You will need to edit the lines 2,3,4 of the below code with your new admin credentials. Change and save.

function wpb_admin_account(){
$user = 'Username';
$pass = 'Password';
$email = '[email protected]';
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');

The new user is now created. Now, I recommend to delete this code from your functions.php for security reasons.

Finally, you can login with your new admin credentials via your website login url: www.example.com/login.

Good Luck!



Please follow and like us:

Display Names differ from Usernames in WordPress?

 

Do users register in your WordPress website and their profile display name is different than their registered Username? Well all you need to do is to add the below code in your functions.php file. This php code will force usernames to be the same as display names. Note that it is always recommended to write the code in the functions.php file located inside a child-theme because you will not lose any modifications you write there, even if you update your theme.

functions.php

function wpse_filter_user_data( $data, $update, $id) {
  if( isset( $data[ 'user_login' ] ) ) {
    $data[ 'display_name' ] = $data[ 'user_login' ];
    return $data;
  }
  $user = get_user_by( 'email', $data[ 'user_email' ] );
  $data[ 'display_name' ] = $user->user_login;
  return $data;
}
add_filter( 'wp_pre_insert_user_data', 'wpse_filter_user_data', 10, 3 );



Please follow and like us: