0

I'm trying to build a function which run when a member is registering on my site.

I am building an extranet using the Simple LDAP login plugin to use my LDAP directory so that users connect on the extranet with the credentials they are used to using with their LDAP account.

Unfortunately, in my LDAP directory, I do not have fields that allow me to fill in the "first_name" and "last_name" fields of a my WordPress users profiles. So, as the email addresses are necessarily constituted as follows: [email protected], this function cuts the email address so that I can retrieve in a table the first name and the last name and, then fill in the first_name and last_name fields of the WordPress user with wp_update_user();

When users log in for the first time, they actually have no account on my WordPress site. The creation of the account is done when the connection to the extranet queries the LDAP directory. That's why I would have liked to use the user_register action hook;

My function is in the functions.php.

Here's the function :

add_action( 'user_register', 'fill_identity' );
function fill_identity(){

    global $current_user; 
    wp_get_current_user();
    get_currentuserinfo();
    $mail_user=$current_user->user_email;
    $mail_user = substr($mail_user,0,-14);
    $mail_user = explode(".", $mail_user);
    $prenom = $mail_user[0] = ucfirst($mail_user[0]);
    $nom = $mail_user[1] = ucfirst($mail_user[1]);
    var_dump($mail_user);
    wp_update_user([
        'ID' => $current_user->ID, 
        'first_name' => $prenom,
        'last_name' => $nom,
    ]);

}

But when someone registers on my site, It changes nothing. I have tested running the fill_identity() function from the header.php and the function works well, it's how I knew the problem was not my function but the add_action. I've tried with others actions and it doesn't work.

I'm not really familiar writing my own hooks, this one is my first. However, my functions.php contains a lot of functions that I've find for some others functionalities and it's works well.

Did you have an idea ?

3
  • 4
    That filter passes the user ID as a parameter, is there a reason you're not using it and instead grabbing the current user info? What exactly does this hook do? It looks like it's mangling the user email somehow. Have you tried asking about what you're originally trying to do rather than how to fix your solution?
    – Tom J Nowell
    Commented Aug 9, 2019 at 14:23
  • Where is your function located? Is it inside functions.php?
    – rits
    Commented Aug 9, 2019 at 14:28
  • what happens if you var_dump(wp_get_current_user());die() . just add after function start
    – Makiomar
    Commented Aug 9, 2019 at 14:38

1 Answer 1

0

HI please check below code.

add_action( 'user_register', 'fill_identity', 10, 1 );
function fill_identity($user_id){

    $current_user = get_userdata($user_id);
    $mail_user = $current_user->user_email;
    $mail_user = substr($mail_user,0,-14);
    $mail_user = explode(".", $mail_user);
    $prenom = $mail_user[0] = ucfirst($mail_user[0]);
    $nom = $mail_user[1] = ucfirst($mail_user[1]);
    var_dump($mail_user);
    wp_update_user(array(
        'ID' => $current_user->ID, 
        'first_name' => $prenom,
        'last_name' => $nom,
    ));
}
1
  • You should explain your code a bit. Commented Aug 13, 2019 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.