【问题标题】:Is there a function that helps populate meta data from an elementor form in Wordpress?是否有帮助从 Wordpress 中的元素或表单填充元数据的功能?
【发布时间】: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
}

【问题讨论】:

    标签: php wordpress elementor


    【解决方案1】:

    您可以简单地创建新的元素或表单字段并为这些字段命名,例如 shown in the picture here

    然后你可以通过 field_id 访问该字段。

    我添加了一个名为 College Name 的字段,并将 ID 作为学校。

    访问后端的字段:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-28
      • 2016-02-29
      • 2016-09-15
      • 2022-08-04
      • 2023-03-23
      • 2013-04-07
      • 2014-06-28
      • 1970-01-01
      相关资源
      最近更新 更多