admin管理员组文章数量:1419702
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 |1 Answer
Reset to default 0HI 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,
));
}
本文标签: hooksaddaction doesn39t work for my function
版权声明:本文标题:hooks - add_action doesn't work for my function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745246363a2649574.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var_dump(wp_get_current_user());die()
. just add after function start – Makiomar Commented Aug 9, 2019 at 14:38