USER_REGISTER挂接中的自定义用户注册字段

时间:2019-05-25 作者:Jiří Peták

我正在尝试使用向管理员发送电子邮件user_register 行动挂钩和wp_mail() 新用户注册时的函数。我创建了一些自定义注册字段,我想通过电子邮件发送它们,但我无法通过get_user_meta() 作用我试着在头版调试这个并使用这个get_user_meta() 函数获取自定义字段,但在函数中。php在我的自定义函数中,缺少字段,而我得到的字段很少:

(昵称、first\\u name、last\\u name、description、rich\\u editing、syntax\\u highlighting、comment\\u shortcuts、admin\\u color、use\\u ssl、show\\u admin\\u bar\\u front、locale、wp\\u capabilities、wp\\u user\\u 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);
}
创建自定义字段时使用:

// 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\' );
保存方式如下:

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\' );
有没有机会让它工作起来?可能是不同的函数或挂钩?我只想在用户创建新帐户时通知管理员(如果有必要,使用WooCommerce),并发送包含该用户所有信息的电子邮件(我的自定义字段)。

1 个回复
SO网友:Jiří Peták

user_register 触发操作后,可以在中访问注册表中的数据$_POST.

Get first_name and last_name on user_register hook

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: