对于自定义注册用户表单,可以使用Advanced Custom Fields to create a front-end form.
您可以使用ACF设置用于注册的自定义字段,并在用户窗体等于Register时设置字段组的位置规则,以及添加/编辑。
我也做了类似的事情acf/pre_save_post 过滤并执行以下操作:
清理输入,向用户注册register_new_user(), 用于处理密码生成和电子邮件通知wp_update_user() 和update_user_meta() 要将适当的自定义ACF字段映射到自定义WP用户配置文件字段,请设置新用户的角色wp_mail() 如有需要,请发送电子邮件给管理员registering a user with ACF 在这里
function my_pre_save_user( $post_id ) {
// If we have an email address, add the user
// This field only exists in the new user field group
if ( isset($_POST[\'acf\'][\'add acf_field_ID here\']) && !empty($_POST[\'acf\'][\'add acf_field_ID here\'])) {
// sanitize our inputs
$sanitized_email = sanitize_email( $_POST[\'acf\'][\'add acf_field_ID here\'] );
$sanitized_firstname = sanitize_text_field( $_POST[\'acf\'][\'add acf_field_ID here\'] );
$sanitized_lastname = sanitize_text_field( $_POST[\'acf\'][\'add acf_field_ID here\'] );
$sanitized_contactnumber = sanitize_text_field( $_POST[\'acf\'][\'add acf_field_ID here\'] );
// Prepare basic user info
$username = $sanitized_email;
$email = $sanitized_email;
$display_name = $sanitized_firstname .\' \'. $sanitized_lastname;
// Register the user and store the ID as $user_id, handles the validation, generates random password and send email notification to user
$user_id = register_new_user( $username, $email );
// If we get an error (eg user already exists)
if( is_wp_error( $user_id ) ) {
// Show the error
echo \'Error: \'.$user_id->get_error_message();
// Exit
exit;
// Good to go
} else {
// get single value from post object
$dmc_get_company_field = $_POST[\'acf\'][\'add acf_field_ID here\'];
$dmc_selected_exhibitor = get_field( $dmc_get_company_field );
// Update the new user\'s info
wp_update_user( array(
\'ID\' => $user_id,
\'first_name\' => $sanitized_firstname,
\'last_name\' => $sanitized_lastname,
\'display_name\' => $display_name
));
// update the new users\'s meta
update_user_meta( $user_id, \'dmc_exhibitor_company_name\', $dmc_get_company_field );
update_user_meta( $user_id, \'dmc_exhibitor_contact_number\', $sanitized_contactnumber );
// update user role
$user_id_role = new WP_User( $user_id );
$user_id_role->set_role( \'contributor\' );
$profile_link = get_edit_user_link( $user_id );
$to = "[add email addresses here]";
$headers[] = \'MIME-Version: 1.0\';
$headers[] = \'Content-Type: text/html; charset=UTF-8\';
$headers[] = \'Reply-To: \'. $username. \' <\'. $email .\'>\';
$subject = "[add email subject here]";
$body = "[add email body here]";
// send the email
wp_mail( $to, $subject, $body, $headers );
// redirect to thankyou page
$redirect_url = get_bloginfo(\'url\') . \'[add URL to redirect to here]\';
wp_redirect( $redirect_url );
// exit this script
exit;
}
} else {
return $post_id;
}
}
add_filter(\'acf/pre_save_post\' , \'my_pre_save_user\' );