• Resolved Beef Supreme

    (@polymathy)


    Hi!

    I have a problem with a dropdown not being saved correctly. The dropdown is part of the registration and there is works perfectly fine. The content of the dropdown is saved as text. However, I also included it within the profile form, so users can update it (on https://mydomain.com/account/)

    So the problem is, when the dropdown is changed in the profile and updated, it is no longer saved as text, but as a number.

    In my backend I created an extra column in the user overview that represents the values. There you can clearly see the wrong values.
    ultimate-member-question

    I used this code to add my extra dropdown field to the profile form on the account page.

    function modify_user_table($columns)
    {
        $new = array();
        foreach ($columns as $key => $title) {
            if ($key == 'posts')
                $new['therapy_center'] = 'Therapiezentrum';
            $new[$key] = $title;
        }
    //    $column['therapy_center'] = 'Therapiezentrum';
        return $new;
    }
    
    add_filter('manage_users_columns', 'modify_user_table');
    
    function modify_user_table_row($val, $column_name, $user_id)
    {
        switch ($column_name) {
            case 'therapy_center' :
                return get_the_author_meta('therapy_center', $user_id);
            default:
        }
        return $val;
    }
    
    add_filter('manage_users_custom_column', 'modify_user_table_row', 10, 3);
    • This topic was modified 4 years, 4 months ago by Beef Supreme.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @polymathy

    How do you add the custom fields to your Account form? Could you share the code so we can review why it returns the number instead of the text?

    Regards,

    Thread Starter Beef Supreme

    (@polymathy)

    Hi @champsupertramp,

    I use this code (the code from my first post is the wrong part of the code): I just create a field in the form editor called “therapy_center” (metakey) and then I add the field with said code:

    /**
     * Add extra fields to user account tab
     */
    function showUMExtraFields()
    {
        $id = um_user('ID');
        $output = '';
        $names = array('si_gender', 'therapy_center');
    
        $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);
    
        echo $output;
    }
    
    add_action('um_after_account_general', 'showUMExtraFields', 100);
    
    /**
     * Get content for extra fields in user account tab
     */
    function getUMFormData()
    {
        $id = um_user('ID');
        $names = array('si_gender', 'therapy_center');
    
        foreach ($names as $name)
            update_user_meta($id, $name, $_POST[$name]);
    }
    
    add_action('um_account_pre_update_profile', 'getUMFormData', 100);

    The code hooks into two filters, that the Ultimate Member plugin provides, um_after_account_general and um_account_pre_update_profile.

    • This reply was modified 4 years, 4 months ago by Beef Supreme.
    • This reply was modified 4 years, 4 months ago by Beef Supreme.
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @polymathy

    And how do you add the select options? is it via Field settings?

    You will need to retrieve the field settings so that you can match the select values with the selected one so you can retrieve the option name for you to save in the user meta.

    Regards,

    Thread Starter Beef Supreme

    (@polymathy)

    For some reason, the select options in the HTML of the account form only have numerical values, like this:

    <option value="0" data-select2-id="16">City</option>

    While in the registration, it looks like this:

    <option value="City" data-select2-id="14">City</option>

    The output from the account form is generated by the UM method “edit_field”. I just give it the $data array related to the field (as you can see in my code), which looks correct to me, especially since it correctly identifies the field as “select” type:

    krumo($data); (Array, 9 elements)
    type (String, 6 characters ) select
    title (String, 15 characters ) Therapiezentrum
    metakey (String, 14 characters ) therapy_center
    options (Array, 5 elements)
    visibility (String, 3 characters ) all
    label (String, 15 characters ) Therapiezentrum
    public (String, 1 characters ) 1
    required (String, 1 characters ) 1
    editable (String, 1 characters ) 1

    I don’t know what the edit_field method does inside. It’s several hundred lines of code, but it returns the HTML code with the value as numbers instead of the actual values.

    • This reply was modified 4 years, 4 months ago by Beef Supreme.
    • This reply was modified 4 years, 4 months ago by Beef Supreme.
    Thread Starter Beef Supreme

    (@polymathy)

    Do I have to pass the edit_field method anything else other than $key and $data in order for the values to be correct?

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @polymathy

    Could you please try the following code if this will change the option values?

    Please try each code at a time just to see if this swaps the values:
    add_filter("um_fields_options_enable_pairs__<insert the field key here>", "__return_true" );

    Or

    add_filter("um_fields_options_enable_pairs__<insert the field key here>", "__return_false" );

    You can add it to your theme’s functions.php or via the code snippet plugin.

    Feel free to re-open this thread by marking this topic as “Not Resolved” so we can get back to you.

    Regards,

    Thread Starter Beef Supreme

    (@polymathy)

    Hi @champsupertramp,

    I tried both codes, but they didn’t change the option values. The only thing that seemingly did change was the data-select2-id attribute and there it was only the actual IDs that changed. But the values remained completely untouched by this.

    Thread Starter Beef Supreme

    (@polymathy)

    Any other idea, what the problem could be?

    Thread Starter Beef Supreme

    (@polymathy)

    I still can’t get this to work properly. Any idea what the problem is here?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Dropdown in Profile Form Is Saved as Number Instead of Text’ is closed to new replies.