【发布时间】:2021-07-03 21:04:49
【问题描述】:
我有一个功能,允许用户从我构建的元素或表单创建一个帐户,但我需要它来创建元数据并将其保存到用户的个人资料中创建帐户后。需要创建的两个元字段是:Date of Birth、Dog's Date of Birth 和 Additional Comments。
请记住,此表单是在前端使用 elementor page builder 构建的,并且在 functions.php 文件中安装了 php 脚本。
这是当前编写的php代码。
// Custom Registration Form Code
add_action( 'elementor_pro/forms/new_record', 'thewpchannel_elementor_form_create_new_user' , 10, 2 );
function thewpchannel_elementor_form_create_new_user($record,$ajax_handler)
{
$form_name = $record->get_form_settings('form_name');
//Check that the form is the "create new user form" if not - stop and return;
if ('Create New User' !== $form_name) {
return;
}
$form_data = $record->get_formatted_data();
$username=$form_data['Username']; //Get the value of the input with the label "User Name"
$password = $form_data['Password']; //Get the value of the input with the label "Password"
$email=$form_data['Email']; //Get the value of the input with the label "Email"
$user = wp_create_user($username,$password,$email); // Create a new user
if (is_wp_error($user)){ // if there was an error creating a new user
$ajax_handler->add_error_message("Failed to create new user: ".$user->get_error_message());
//add the message
$ajax_handler->is_success = false;
return;
}
$first_name=$form_data["First Name"]; //Get the value of the input with the label "First Name"
$last_name=$form_data["Last Name"]; //Get the value of the input with the label "Last Name"
$user_phone=$form_data['Phone Number']; //Get the value of the input with the label "Phone Number"
$more_notes=$form_data['Anything Else?']; //Get the value of the input with the label "Anything Else?"
wp_update_user(array("ID"=>$user,"first_name"=>$first_name,"last_name"=>$last_name,"user_phone"=>$user_phone,"form_fields[dog_dob]"=>$more_notes)); // Update the user with the first name and last name
}
【问题讨论】: