admin管理员组文章数量:1130349
We have a customized the registration process. Users register themselves. When a user registers, some meta values are updated based on the $user_id. Now we need to send an email.
We have used action hook named user_register to send the email. When a user registers, an email will be sent to that user. The problem is that the meta value is not being sent even when I have gave the hook a priority of 100.
How can I fetch the meta value of that particular user using user_register hook so i can send them in an email?
Here's the registration code:
add_action( 'user_register', 'sendMailM', 99999, 1 );
function sendMailM( $user_id ) {
$title = "Title";
$from = "[email protected]";
global $wpdb,
$password;
$user = new WP_User( $user_id );
$user_login = stripslashes( $user->user_login );
$user_email = stripslashes( $user->user_email );
$companyCreatedUserP4 = get_user_meta( $user_id, 'companyId', true );
if( ! empty( $companyCreatedUserP4 ) )
{
$current_companyP4 = new WP_User( $companyCreatedUserP4 );
$companyEmailP4 = $current_companyP4->user_email;
$messageAdmin = 'New User ' . $user_login . ' is registered on your site under ' . $current_companyP4->user_firstname . ' company.';
$messageCompanyP4 = 'A new user with a user name: ' . $user_login . ' was registered under your company.';
}
else
{
$messageAdmin = 'New User ' . $user_login . ' is registered on your site.';
}
$message = "<p>You are now registered . Your user name and password are included in this email. </p>";
$message .= "<p>" . sprintf( __( 'Username: %s' ), $user_login ) . "\r\n\r\n</p>";
$message .= "<p>" . sprintf( __( 'Password: %s' ), $passwor d) . "\r\n</p>";
$headers = 'From: ' . $title . '<' . $from . ">\r\nReply-To: " . $from;
add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
wp_mail( get_option( 'admin_email' ), 'New User Registration ', $messageAdmin, $headers ); /*admin*/
if( ! empty( $companyEmailP4 ) )
wp_mail( $companyEmailP4, 'New User Registration ', $messageCompanyP4, $headers ); /*user*/
}
This is the code to create a new user and update the meta value:
$user = wp_insert_user( $userdata );
update_user_meta( $user, 'companyId', 350 );
We have a customized the registration process. Users register themselves. When a user registers, some meta values are updated based on the $user_id. Now we need to send an email.
We have used action hook named user_register to send the email. When a user registers, an email will be sent to that user. The problem is that the meta value is not being sent even when I have gave the hook a priority of 100.
How can I fetch the meta value of that particular user using user_register hook so i can send them in an email?
Here's the registration code:
add_action( 'user_register', 'sendMailM', 99999, 1 );
function sendMailM( $user_id ) {
$title = "Title";
$from = "[email protected]";
global $wpdb,
$password;
$user = new WP_User( $user_id );
$user_login = stripslashes( $user->user_login );
$user_email = stripslashes( $user->user_email );
$companyCreatedUserP4 = get_user_meta( $user_id, 'companyId', true );
if( ! empty( $companyCreatedUserP4 ) )
{
$current_companyP4 = new WP_User( $companyCreatedUserP4 );
$companyEmailP4 = $current_companyP4->user_email;
$messageAdmin = 'New User ' . $user_login . ' is registered on your site under ' . $current_companyP4->user_firstname . ' company.';
$messageCompanyP4 = 'A new user with a user name: ' . $user_login . ' was registered under your company.';
}
else
{
$messageAdmin = 'New User ' . $user_login . ' is registered on your site.';
}
$message = "<p>You are now registered . Your user name and password are included in this email. </p>";
$message .= "<p>" . sprintf( __( 'Username: %s' ), $user_login ) . "\r\n\r\n</p>";
$message .= "<p>" . sprintf( __( 'Password: %s' ), $passwor d) . "\r\n</p>";
$headers = 'From: ' . $title . '<' . $from . ">\r\nReply-To: " . $from;
add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
wp_mail( get_option( 'admin_email' ), 'New User Registration ', $messageAdmin, $headers ); /*admin*/
if( ! empty( $companyEmailP4 ) )
wp_mail( $companyEmailP4, 'New User Registration ', $messageCompanyP4, $headers ); /*user*/
}
This is the code to create a new user and update the meta value:
$user = wp_insert_user( $userdata );
update_user_meta( $user, 'companyId', 350 );
Share
Improve this question
edited Nov 21, 2015 at 5:42
Howdy_McGee♦
20.9k24 gold badges91 silver badges177 bronze badges
asked Nov 21, 2015 at 4:51
MominMomin
231 silver badge4 bronze badges
13
|
Show 8 more comments
1 Answer
Reset to default 2Read de Codex entry for user_register action, it says:
Not all user meta data has been stored in the database when this action is triggered.
Note that doing:
$user = wp_insert_user( $userdata );
update_user_meta( $user, 'companyId', 350 );
Follow this sequence: insert user -> run user_register action -> rung your update user meta function. So, your custom user meta is not available in user_register action.
So, instead of updating user meta data after wp_insert_user(), you could do it inside the user_register action:
add_action( 'user_register', 'sendMailM' );
function sendMailM( $user_id ) {
// Note: $_POST data is available here,
// just in case you need to update user meta based on form input,
// for example, $_POST['companyId']
update_user_meta( $user_id, 'companyId', 350 );
$title = "Title";
$from = "[email protected]";
global $wpdb,
$password;
$user = new WP_User( $user_id );
$user_login = stripslashes( $user->user_login );
$user_email = stripslashes( $user->user_email );
// You can now access to previously updated user meta
// Or get the companyId directly from $_POST input if needed
$companyCreatedUserP4 = get_user_meta( $user_id, 'companyId', true );
if( ! empty( $companyCreatedUserP4 ) )
{
$current_companyP4 = new WP_User( $companyCreatedUserP4 );
$companyEmailP4 = $current_companyP4->user_email;
$messageAdmin = 'New User ' . $user_login . ' is registered on your site under ' . $current_companyP4->user_firstname . ' company.';
$messageCompanyP4 = 'A new user with a user name: ' . $user_login . ' was registered under your company.';
}
else
{
$messageAdmin = 'New User ' . $user_login . ' is registered on your site.';
}
$message = "<p>You are now registered . Your user name and password are included in this email. </p>";
$message .= "<p>" . sprintf( __( 'Username: %s' ), $user_login ) . "\r\n\r\n</p>";
$message .= "<p>" . sprintf( __( 'Password: %s' ), $passwor d) . "\r\n</p>";
$headers = 'From: ' . $title . '<' . $from . ">\r\nReply-To: " . $from;
add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
wp_mail( get_option( 'admin_email' ), 'New User Registration ', $messageAdmin, $headers ); /*admin*/
if( ! empty( $companyEmailP4 ) )
wp_mail( $companyEmailP4, 'New User Registration ', $messageCompanyP4, $headers ); /*user*/
}
We have a customized the registration process. Users register themselves. When a user registers, some meta values are updated based on the $user_id. Now we need to send an email.
We have used action hook named user_register to send the email. When a user registers, an email will be sent to that user. The problem is that the meta value is not being sent even when I have gave the hook a priority of 100.
How can I fetch the meta value of that particular user using user_register hook so i can send them in an email?
Here's the registration code:
add_action( 'user_register', 'sendMailM', 99999, 1 );
function sendMailM( $user_id ) {
$title = "Title";
$from = "[email protected]";
global $wpdb,
$password;
$user = new WP_User( $user_id );
$user_login = stripslashes( $user->user_login );
$user_email = stripslashes( $user->user_email );
$companyCreatedUserP4 = get_user_meta( $user_id, 'companyId', true );
if( ! empty( $companyCreatedUserP4 ) )
{
$current_companyP4 = new WP_User( $companyCreatedUserP4 );
$companyEmailP4 = $current_companyP4->user_email;
$messageAdmin = 'New User ' . $user_login . ' is registered on your site under ' . $current_companyP4->user_firstname . ' company.';
$messageCompanyP4 = 'A new user with a user name: ' . $user_login . ' was registered under your company.';
}
else
{
$messageAdmin = 'New User ' . $user_login . ' is registered on your site.';
}
$message = "<p>You are now registered . Your user name and password are included in this email. </p>";
$message .= "<p>" . sprintf( __( 'Username: %s' ), $user_login ) . "\r\n\r\n</p>";
$message .= "<p>" . sprintf( __( 'Password: %s' ), $passwor d) . "\r\n</p>";
$headers = 'From: ' . $title . '<' . $from . ">\r\nReply-To: " . $from;
add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
wp_mail( get_option( 'admin_email' ), 'New User Registration ', $messageAdmin, $headers ); /*admin*/
if( ! empty( $companyEmailP4 ) )
wp_mail( $companyEmailP4, 'New User Registration ', $messageCompanyP4, $headers ); /*user*/
}
This is the code to create a new user and update the meta value:
$user = wp_insert_user( $userdata );
update_user_meta( $user, 'companyId', 350 );
We have a customized the registration process. Users register themselves. When a user registers, some meta values are updated based on the $user_id. Now we need to send an email.
We have used action hook named user_register to send the email. When a user registers, an email will be sent to that user. The problem is that the meta value is not being sent even when I have gave the hook a priority of 100.
How can I fetch the meta value of that particular user using user_register hook so i can send them in an email?
Here's the registration code:
add_action( 'user_register', 'sendMailM', 99999, 1 );
function sendMailM( $user_id ) {
$title = "Title";
$from = "[email protected]";
global $wpdb,
$password;
$user = new WP_User( $user_id );
$user_login = stripslashes( $user->user_login );
$user_email = stripslashes( $user->user_email );
$companyCreatedUserP4 = get_user_meta( $user_id, 'companyId', true );
if( ! empty( $companyCreatedUserP4 ) )
{
$current_companyP4 = new WP_User( $companyCreatedUserP4 );
$companyEmailP4 = $current_companyP4->user_email;
$messageAdmin = 'New User ' . $user_login . ' is registered on your site under ' . $current_companyP4->user_firstname . ' company.';
$messageCompanyP4 = 'A new user with a user name: ' . $user_login . ' was registered under your company.';
}
else
{
$messageAdmin = 'New User ' . $user_login . ' is registered on your site.';
}
$message = "<p>You are now registered . Your user name and password are included in this email. </p>";
$message .= "<p>" . sprintf( __( 'Username: %s' ), $user_login ) . "\r\n\r\n</p>";
$message .= "<p>" . sprintf( __( 'Password: %s' ), $passwor d) . "\r\n</p>";
$headers = 'From: ' . $title . '<' . $from . ">\r\nReply-To: " . $from;
add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
wp_mail( get_option( 'admin_email' ), 'New User Registration ', $messageAdmin, $headers ); /*admin*/
if( ! empty( $companyEmailP4 ) )
wp_mail( $companyEmailP4, 'New User Registration ', $messageCompanyP4, $headers ); /*user*/
}
This is the code to create a new user and update the meta value:
$user = wp_insert_user( $userdata );
update_user_meta( $user, 'companyId', 350 );
Share
Improve this question
edited Nov 21, 2015 at 5:42
Howdy_McGee♦
20.9k24 gold badges91 silver badges177 bronze badges
asked Nov 21, 2015 at 4:51
MominMomin
231 silver badge4 bronze badges
13
-
Are you using the
get_user_meta()function? Looks like the$user_idgets passed to theuser_registerhook – Howdy_McGee ♦ Commented Nov 21, 2015 at 5:03 -
Yes.I am using get_user_meta().
$companyCreatedUserP4 = get_user_meta($user_id, 'key', true );– Momin Commented Nov 21, 2015 at 5:08 - but result was empty, but it should not be...When i pass static previously created user id, then i am getting the result. – Momin Commented Nov 21, 2015 at 5:09
- Are you 100% sure that you're using the correct key name? Did you look in the database to verify? – Howdy_McGee ♦ Commented Nov 21, 2015 at 5:10
-
2
I've edited your question and so it's easier to understand and the code is easier to read. Please try not to put code blocks in the comments here, instead edit your question and add in that code as more detail explaining what it is and why it's important. Also, when adding code to questions there's a button which looks like this
{}- highlight your code and click that button and it will put it into a nice code block as you see now. This makes it much easier for people to read and for other to help you. I think I have a solution to your problem, I'm writing up an answer now just give me a sec – Howdy_McGee ♦ Commented Nov 21, 2015 at 5:47
1 Answer
Reset to default 2Read de Codex entry for user_register action, it says:
Not all user meta data has been stored in the database when this action is triggered.
Note that doing:
$user = wp_insert_user( $userdata );
update_user_meta( $user, 'companyId', 350 );
Follow this sequence: insert user -> run user_register action -> rung your update user meta function. So, your custom user meta is not available in user_register action.
So, instead of updating user meta data after wp_insert_user(), you could do it inside the user_register action:
add_action( 'user_register', 'sendMailM' );
function sendMailM( $user_id ) {
// Note: $_POST data is available here,
// just in case you need to update user meta based on form input,
// for example, $_POST['companyId']
update_user_meta( $user_id, 'companyId', 350 );
$title = "Title";
$from = "[email protected]";
global $wpdb,
$password;
$user = new WP_User( $user_id );
$user_login = stripslashes( $user->user_login );
$user_email = stripslashes( $user->user_email );
// You can now access to previously updated user meta
// Or get the companyId directly from $_POST input if needed
$companyCreatedUserP4 = get_user_meta( $user_id, 'companyId', true );
if( ! empty( $companyCreatedUserP4 ) )
{
$current_companyP4 = new WP_User( $companyCreatedUserP4 );
$companyEmailP4 = $current_companyP4->user_email;
$messageAdmin = 'New User ' . $user_login . ' is registered on your site under ' . $current_companyP4->user_firstname . ' company.';
$messageCompanyP4 = 'A new user with a user name: ' . $user_login . ' was registered under your company.';
}
else
{
$messageAdmin = 'New User ' . $user_login . ' is registered on your site.';
}
$message = "<p>You are now registered . Your user name and password are included in this email. </p>";
$message .= "<p>" . sprintf( __( 'Username: %s' ), $user_login ) . "\r\n\r\n</p>";
$message .= "<p>" . sprintf( __( 'Password: %s' ), $passwor d) . "\r\n</p>";
$headers = 'From: ' . $title . '<' . $from . ">\r\nReply-To: " . $from;
add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
wp_mail( get_option( 'admin_email' ), 'New User Registration ', $messageAdmin, $headers ); /*admin*/
if( ! empty( $companyEmailP4 ) )
wp_mail( $companyEmailP4, 'New User Registration ', $messageCompanyP4, $headers ); /*user*/
}
本文标签: How to get usermeta value for new user regsitered
版权声明:本文标题:How to get user_meta value for new user regsitered? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749231012a2336485.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


get_user_meta()function? Looks like the$user_idgets passed to theuser_registerhook – Howdy_McGee ♦ Commented Nov 21, 2015 at 5:03$companyCreatedUserP4 = get_user_meta($user_id, 'key', true );– Momin Commented Nov 21, 2015 at 5:08{}- highlight your code and click that button and it will put it into a nice code block as you see now. This makes it much easier for people to read and for other to help you. I think I have a solution to your problem, I'm writing up an answer now just give me a sec – Howdy_McGee ♦ Commented Nov 21, 2015 at 5:47