email - Custom user registration fields in user_register hook

admin2025-01-07  4

I am trying to send an email to administrator with user_register action hook and wp_mail() function when a new user registers. I created some custom registration fields and I want send them via e-mail, but I cannot access them via the get_user_meta() function. I tried to debug this in front-page and using this get_user_meta() function gets me my custom fields, but in functions.php in my custom function, fields are missing and I get only few fields:

(nickname,first_name,last_name,description,rich_editing,syntax_highlighting,comment_shortcuts,admin_color,use_ssl,show_admin_bar_front,locale,wp_capabilities,wp_user_level)

add_action( 'user_register', 'so27450945_user_register', 10, 1 );
function so27450945_user_register( $user_id )
{
    $user = get_user_by( 'id', $user_id );
    $user_meta = get_user_meta( $user_id );

    $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );

    $message  = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n";
    $message .= sprintf( __( 'Username: %s'), $user->user_login ) . "\r\n\r\n";
    $message .= sprintf( __( 'E-mail: %s'), $user->user_email ) . "\r\n";

    $message .= $user_meta['reg_company'][0];


    @wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] New User Registration' ), $blogname ), $message);
}

Custom fields created with:

// Adding Company information on account registration
function wooc_extra_company_register_fields() {?>
  <p class="form-row">
    <input type="checkbox" class="input-checkbox" name="reg_company" id="reg_company" <?php if ( ! empty( $_POST['reg_company'] ) ) echo "checked"; ?> /><?php _e( 'Registrovat velkoobchod?', 'vinabg' ); ?>
  </p>
 }
 add_action( 'woocommerce_register_form', 'wooc_extra_company_register_fields' );

And saved like:

function wooc_save_extra_register_fields( $customer_id ) {
      if ( isset( $_POST['reg_company'] ) ) {
             update_user_meta( $customer_id, 'reg_company', $_POST['reg_company'] );
      }
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

Is any chance to get this working? Maybe different functions or hooks? I'd like to just notify administrator when user created new account (using WooCommerce if that matters), and send email with all information about that user (my custom fields).

I am trying to send an email to administrator with user_register action hook and wp_mail() function when a new user registers. I created some custom registration fields and I want send them via e-mail, but I cannot access them via the get_user_meta() function. I tried to debug this in front-page and using this get_user_meta() function gets me my custom fields, but in functions.php in my custom function, fields are missing and I get only few fields:

(nickname,first_name,last_name,description,rich_editing,syntax_highlighting,comment_shortcuts,admin_color,use_ssl,show_admin_bar_front,locale,wp_capabilities,wp_user_level)

add_action( 'user_register', 'so27450945_user_register', 10, 1 );
function so27450945_user_register( $user_id )
{
    $user = get_user_by( 'id', $user_id );
    $user_meta = get_user_meta( $user_id );

    $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );

    $message  = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n";
    $message .= sprintf( __( 'Username: %s'), $user->user_login ) . "\r\n\r\n";
    $message .= sprintf( __( 'E-mail: %s'), $user->user_email ) . "\r\n";

    $message .= $user_meta['reg_company'][0];


    @wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] New User Registration' ), $blogname ), $message);
}

Custom fields created with:

// Adding Company information on account registration
function wooc_extra_company_register_fields() {?>
  <p class="form-row">
    <input type="checkbox" class="input-checkbox" name="reg_company" id="reg_company" <?php if ( ! empty( $_POST['reg_company'] ) ) echo "checked"; ?> /><?php _e( 'Registrovat velkoobchod?', 'vinabg' ); ?>
  </p>
 }
 add_action( 'woocommerce_register_form', 'wooc_extra_company_register_fields' );

And saved like:

function wooc_save_extra_register_fields( $customer_id ) {
      if ( isset( $_POST['reg_company'] ) ) {
             update_user_meta( $customer_id, 'reg_company', $_POST['reg_company'] );
      }
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

Is any chance to get this working? Maybe different functions or hooks? I'd like to just notify administrator when user created new account (using WooCommerce if that matters), and send email with all information about that user (my custom fields).

Share Improve this question edited Jun 8, 2019 at 21:42 butlerblog 5,0713 gold badges26 silver badges42 bronze badges asked May 25, 2019 at 20:29 Jiří PetákJiří Peták 11 silver badge1 bronze badge 1
  • All the metadata is not stored in the database when the user_register hook is triggered as from the documentation – Tariq Khan Commented Oct 13, 2020 at 12:52
Add a comment  | 

1 Answer 1

Reset to default 0

When the user_register action is triggered, data from the registration form can be accessed in $_POST.

Get first_name and last_name on user_register hook

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736261355a735.html

最新回复(0)