admin管理员组文章数量:1122833
I am currently using the below code to add some meta fields to my users and would love to include these in their registration email, but it appears they are not created before the email is sent.
// action hook
add_action( 'user_register', function ( $user_id ) {
$pincode = wp_rand(1000,9999);
// set pincode
update_user_meta($user_id, 'pincode', $pincode);
// update user to have pin created
update_user_meta($user_id, 'pincreated', 1);
// set attempts to 0
update_user_meta( $user_id, 'attempts', 0);
});
I have also now tried hooking in to the invite email, but this one didn't seem to create the pincode for me.
// action hook
function add_user_pin( $user_id ) {
// do not set pin if user has already logged in once
if ( get_user_meta( $user_id, 'pincreated', TRUE ) ) {
return;
}
$pincode = wp_rand(1000,9999);
// setpincode if logging in first time
update_user_meta($user_id, 'pincode', $pincode);
// update user to have completed first login
update_user_meta( $user_id, 'pincreated', 1 );
// set attempts to 0
update_user_meta( $user_id, 'attempts', 0);
};
add_filter( 'invited_user_email', 'add_user_pin',10,4);
I have also tried another variation of below, but this doesn't seem to create the pincode at all
// action hook
add_action( 'edit_user_created_user', function ( $user_id ) {
// do not set pin if user has already logged in once
// if ( current_user_can( 'administrator' ) || get_user_meta( $user_id, 'pincreated', TRUE ) ) {
// return;
// }
$pincode = wp_rand(1000,9999);
// setpincode if logging in first time
update_user_meta($user_id, 'pincode', $pincode);
// update user to have completed first login
update_user_meta( $user_id, 'pincreated', 1 );
// set attempts to 0
update_user_meta( $user_id, 'attempts', 0);
});
I am currently using the below code to add some meta fields to my users and would love to include these in their registration email, but it appears they are not created before the email is sent.
// action hook
add_action( 'user_register', function ( $user_id ) {
$pincode = wp_rand(1000,9999);
// set pincode
update_user_meta($user_id, 'pincode', $pincode);
// update user to have pin created
update_user_meta($user_id, 'pincreated', 1);
// set attempts to 0
update_user_meta( $user_id, 'attempts', 0);
});
I have also now tried hooking in to the invite email, but this one didn't seem to create the pincode for me.
// action hook
function add_user_pin( $user_id ) {
// do not set pin if user has already logged in once
if ( get_user_meta( $user_id, 'pincreated', TRUE ) ) {
return;
}
$pincode = wp_rand(1000,9999);
// setpincode if logging in first time
update_user_meta($user_id, 'pincode', $pincode);
// update user to have completed first login
update_user_meta( $user_id, 'pincreated', 1 );
// set attempts to 0
update_user_meta( $user_id, 'attempts', 0);
};
add_filter( 'invited_user_email', 'add_user_pin',10,4);
I have also tried another variation of below, but this doesn't seem to create the pincode at all
// action hook
add_action( 'edit_user_created_user', function ( $user_id ) {
// do not set pin if user has already logged in once
// if ( current_user_can( 'administrator' ) || get_user_meta( $user_id, 'pincreated', TRUE ) ) {
// return;
// }
$pincode = wp_rand(1000,9999);
// setpincode if logging in first time
update_user_meta($user_id, 'pincode', $pincode);
// update user to have completed first login
update_user_meta( $user_id, 'pincreated', 1 );
// set attempts to 0
update_user_meta( $user_id, 'attempts', 0);
});
Share
Improve this question
edited May 7, 2024 at 3:07
Matt Holt
asked Apr 30, 2024 at 3:31
Matt HoltMatt Holt
115 bronze badges
1
|
2 Answers
Reset to default 0You'll need to use an earlier hook. I'd suggest new_user_email_content
, which will also let you filter the contents of the email.
I was able to figure this out. You can add your custom meta to the hook specifically designed for this. insert_custom_user_meta
This is then created and added to the user before the email is sent.
add_filter( 'insert_custom_user_meta', 'wp_insert_custom_user_meta_filter', 10, 4 );
function wp_insert_custom_user_meta_filter( $custom_meta, $user, $update, $userdata ){
// Be sure to only add a new pin if creating the user by performing only when $update is false
if (! $update) {
$pincode = wp_rand(1000,9999);
$custom_meta = array(
'pincode' => $pincode,
'pincreated' => '1',
'attempts' => '1'
);
return $custom_meta;
} else {
return $custom_meta;
}
}
本文标签: Include New Meta Field In New User Registration Email
版权声明:本文标题:Include New Meta Field In New User Registration Email 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308044a1933521.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
edit_user_created_user
hook -- it fires at the very end of the user creation process. – Pat J Commented May 1, 2024 at 18:43