hooks - add_action doesn't work for my function

I'm trying to build a function which run when a member is registering on my site.I am building an extranet using th

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 ?

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 ?

Share Improve this question edited Aug 13, 2019 at 14:29 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Aug 9, 2019 at 14:00 RiyacteRiyacte 1 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
Add a comment  | 

1 Answer 1

Reset to default 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,
    ));
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745246364a4618429.html

相关推荐

  • hooks - add_action doesn't work for my function

    I'm trying to build a function which run when a member is registering on my site.I am building an extranet using th

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信