skip to Main Content

I am using the plugin "Ultimate Member" for WordPress, It is working fine… I want some additional functionality where the admin has the ability to change certain data (like gender, occupation, country, etc.) from the admin panel (edit user page). With some help from some blog posts I put together some code which is able to display edit fields of the relevant/target user on the edit account page.

The issue is that certain fields like gender and country are not showing their current values and none of the fields update when the "user button" is clicked.

these are the custom meta fields displayed in the edit account page

Here is the code I used on ./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]);
        }
    }
}

Can somebody help me figure out what I am doing wrong?

2

Answers


  1. Chosen as BEST ANSWER

    There is no way to display select boxes from Ultimate Member in another form/page. I came up with a solution... but it still isn't good enough, it doesn't update the select boxes...

    //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');
    
    

    Let me know if I am doing anything wrong. Thanks in advance.


  2. I’m not sur if it’s right. I expanded your code to use for checkboxes, and seems ok

    //display fields
    function showUMExtraFields()
    {
        ob_start();
        
        //Only reading $disable = "disabled", else leave just ""
        $disable = "disabled";
    
        $id = um_user('ID');
        // $id = UM()->user()->target_id;
        $output = '<table class="form-table">';
        $output = $output.'<tr><th><label>Extra profile information</label></th>';
        $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 .= 
                 "<td>
                    <span class='um-field um-field-select um-field-$key um-field-select um-field-type_select' data-key='$key'>$select_title:</span>
                        <div class='um-field-label'> 
                            <label for='$key'></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>
                </td>";
            
            //if it is a checkbox
            }elseif($data['type'] == 'checkbox') {
            
                $check_value = get_user_meta( $id, $key)[0];
                $check_title =  $data['title'];
                $check_meta_key = $data['metakey'];
                $output .= 
                "
                    <td>
                        <span class='usergroup-category-name'>$check_title</span><br/>";
                foreach($data['options'] as $option){
                    $output .="<input name='$key"."[]' id='$key"."-$option' type='checkbox' value='$option'";
                    if  (in_array($option, $check_value)){
                        $output .='checked="checked"';}
                    $output .= $disable."/> <label>$option </label><br/>";
                }
                $output .= "</td>";  
             }else{
                //if not a select box (the default way)
                $output .= UM()->fields()->edit_field($key, $data);         
            }       
        }
    
        $output .= '<tr></table>';
    
        $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');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search