NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Adding a User via Functions.php
Easy

---- Last modified : Oct 01 2022

Estimated reading time : 1 minute, 4 seconds - 93 words

With this tutorial, we will show you how to add a WordPress user via the functions.php file. This how-to is pretty useful in case you can’t access your admin section of WordPress for whatever reason may be.

While you can always do a password reset, you can easily add a simply line of code within your functions.php file via an FTP software:

Adding a WordPress user via functions.php

function nomdevotrefuonction(){
  /// Ici mettez votre nom utilisateur sans majuscules
  $user = 'Username';
  /// Ici mettez votre mot de passe
  $pass = 'Password';
  /// Ici mettez votre courriel
  $email = 'email@domain.com';
 
  /// Condition que utilisateur existe pas on le créé
  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','nomdevotrefuonction');

Once you’ll save the changes in the functions.php file, the new user will be created thus allowing you to log back in.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>