php 有没有一个函数可以帮助从WordPress中的元素或表单填充 meta数据?

7gcisfzg  于 2022-11-28  发布在  PHP
关注(0)|答案(1)|浏览(98)

我有一个功能,允许用户从我创建的元素或表单创建帐户,但我需要它在创建帐户后创建 meta数据并将其保存到用户的配置文件中。需要创建的两个元字段是:出生日期、犬出生日期和其他备注。
请记住,这个表单是用elementor页面生成器在前端构建的,php脚本安装在functions.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
}
vhmi4jdf

vhmi4jdf1#

您可以简单地创建新的元素或表单域,并为这些域给予,如shown in the picture here
然后,您可以通过field_id访问该字段。
我添加了一个名为College Name的字段,并将id指定为school。
要在后端访问字段:

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)
{
    $raw_fields = $record->get( 'fields' );
    $school_name = $raw_fields['school'];

    //process the data here
}

相关问题