0

我正在为 WordPress 使用插件“Ultimate Member”,它运行良好......我想要一些额外的功能,让管理员能够从管理面板更改某些数据(如性别、职业、国家等)(编辑用户页面)。在一些博客文章的帮助下,我整理了一些代码,这些代码能够在编辑帐户页面上显示相关/目标用户的编辑字段。

问题是某些字段(例如性别和国家/地区)没有显示其当前值,并且在单击“用户按钮”时没有任何字段更新。

这些是编辑帐户页面中显示的自定义元字段

这是我在 ./wp-content/themes/twentytwentyone/functions.php 上使用的代码:

add_action('edit_user_profile', 'showUMExtraFields', 100);

function showUMExtraFields()
{
    ob_start();

    $id = um_user('ID');
    $output = '<div class="um-field">';
    $names = array("user_login", 'birth_date', "gender", "country", "user_url", "cool_input");  // the meta names of the feilds I want to display

    $fields = array();
    foreach ($names as $name) {
        $fields[$name] = UM()->builtin()->get_specific_field($name);
    }
    $fields = apply_filters('um_account_secure_fields', $fields, $id);
    foreach ($fields as $key => $data) {
        $output .= UM()->fields()->edit_field($key, $data);
    }

    $output .= '</div>';

    $output .= ob_get_contents();
    ob_end_clean();
    echo $output;
}

add_action('edit_user_profile_update', 'getUMFormData');

function getUMFormData()
{
    $id = um_user('ID');
    $names = array("user_login", 'birth_date', "gender", "country", "user_url", "cool_input");  // the meta names of the feilds I want to display

    foreach ($names as $name) {
        if (!empty($secure_fields)) {
            update_user_meta($id, $name, $_POST[$name]);
        }
    }
}

有人可以帮我弄清楚我做错了什么吗?

4

1 回答 1

0

无法在另一个表单/页面中显示来自 Ultimate Member 的选择框。我想出了一个解决方案......但它仍然不够好,它没有更新选择框......

//fields that you want to display... meta_keys
function custom_feild_meta(){
    return $feild_meta = array("first_name", 'last_name', "GrandFather_Name", "Brach_Name", "Family_Name", "Family_Name_41",  "mobile_number_38", "user_email", "mobile_number", "Secondary_System", "mobile_number_38_37_37", "mobile_number_38_37", "mobile_number_38_37_37_37", "Guardian_Name", "phone_number", "country", "City_26_27_29_30_31_32_33_34_35_36", "City_26", "City", "City_26_27_29", "City_26_27", "City_26_27_29_30_31", "City_26_27_29_30_31_32", "City_26_27_29_30", "City_26_27_29_30_31_32_33", "City_26_27_29_30_31_32_33_34", "City_26_27_29_30_31_32_33_34_35", "City_26_27_28", "City_26_27_29_30_31_32_33_34_35_36_37", "Note_42");  //feilds feilds meta name to display
}


//display fields
function showUMExtraFields()
{

    
    print('<h3>Extra profile information</h3>');
    
    ob_start();
    

    $id = um_user('ID');
    // $id = UM()->user()->target_id;
    $output = '<div class="um-field">';
    $tame;
    $names = custom_feild_meta();  
    
    $fields = array();
    foreach ($names as $name) {
        $fields[$name] = UM()->builtin()->get_specific_field($name);
    }
    $fields = apply_filters('um_account_secure_fields', $fields, $id);
    foreach ($fields as $key => $data) {
        
        //if it is a select box
        if($data['type'] == 'select'){
            
            $select_value = get_user_meta( $id, $key)[0];
            $select_title =  $data['title'];
            $select_meta_key = $data['metakey'];
            
            
             $output .= "<div id='um_field__$key' class='um-field um-field-select  um-field-$key um-field-select um-field-type_select' data-key='$key'> <div class='um-field-label'> <label for='$key'>$select_title</label> <div class='um-clear'></div> </div> <div class='um-field-area  '> <select data-default='$value' name='$select_meta_key' id='$key' data-validate='' data-key='$key' class='um-form-field valid um-s1 ' style='width: 100%'>";
             
             foreach($data['options'] as $option){
                 $output .= "<option value='$option' ($option == $select_value ? 'selected' : '')>$option</option>";
             }

             $output .= "</select> </div> </div>";  
        }else{
            //if not a select box (the default way)
            $output .= UM()->fields()->edit_field($key, $data);         
        }       
    }

    $output .= '</div>';

    $output .= ob_get_contents();
    ob_end_clean();
    echo $output;
    echo $tame;
}


//update/save field date, works only for default fields not select boxes
function getUMFormData($user_id)
{
    if (!current_user_can('edit_user', $user_id))
        return false;

    $meta_number = 0;
    $custom_meta_fields = custom_feild_meta();
    foreach ($custom_meta_fields as $meta_field_name) {
        $meta_number++;
        update_user_meta($user_id, $meta_field_name, $_POST[$meta_field_name]);
    }
}


add_action('show_user_profile', 'showUMExtraFields');
add_action('edit_user_profile', 'showUMExtraFields');
add_action('edit_user_profile_update', 'getUMFormData');
add_action('personal_options_update', 'getUMFormData');

如果我做错了什么,请告诉我。提前致谢。

于 2021-06-13T15:03:42.463 回答