• Resolved celinehag

    (@celinehag)


    Hi UM-Support Team,

    I have custom fields on the user account tab.

    I added these fields using these lines in functions.php:

    /**
    * Add extra fields to user account tab
    */

    function showUMExtraFields()
    {
    $id = um_user(‘ID’);
    $output = ”;
    $names = array(‘salutation’, ‘first_name’, ‘last_name’, ‘company’, ‘job’);

    $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(‘salutation’, ‘first_name’, ‘last_name’, ‘company’, ‘job’);

    foreach ($names as $name)
    update_user_meta($id, $name, $_POST[$name]);
    }
    add_action(‘um_account_pre_update_profile’, ‘getUMFormData’, 100);`

    The fields are updated when a user updates his profile. So that seems to be working. Only when a user changes his password, all these additional fields are deleted while email and username remain unchanged. Somehow the password change process deletes all entries in custom fields.

    I tried getUMFormData with other hooks like um_after_password_reset_fields, etc. but couldn’t manage to get it right.

    Can you pls point me in the right direction to solve this riddle?

    Thanks for your support.

    Hagbard

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter celinehag

    (@celinehag)

    @missveronicatv

    When I replace the code I’ve been using above with the snippet you pointed out – accordingly using my own custom fields as args – no custom fields at all show up on the account tab.

    My setup ist latest version of WP and UM-Plugin.

    Any ideas why this might not be working? Any additional settings needed?

    Thread Starter celinehag

    (@celinehag)

    I just tried using the code snippet with the args in the example instead of my own. Strangely the fields birth_date, country, languages show up and are able to store data entered?? These fields are not in the UM default registration form nor anywhere else in my configuration.

    @celinehag

    Can you post your version of the code snippet and use the CODE formatting.

    Thread Starter celinehag

    (@celinehag)

    @missveronicatv

    thanks for responding!

    I am using the code snippet exactly as shown in the link you sent me:

    https://docs.ultimatemember.com/article/1770-extend-the-account-page-with-custom-fields

    /**
     * Add custom fields to the main tab "Account" under the "E-mail Address" field.
     * Add this code to the file functions.php in the active theme directory.
     */
    function um_account_custom_fields( $args, $shortcode_args ) {
    
    	// Fields Meta Keys.
    	$args .= ',birth_date';
    	$args .= ',country';
    	$args .= ',languages';
    
    	return $args;
    }
    add_filter( 'um_account_tab_general_fields', 'um_account_custom_fields', 10, 2 );

    Like I described in my recent post:

    Strangely the fields birth_date, country, languages show up and are able to store data entered?? These fields are not in the UM default registration form nor anywhere else in my configuration.

    In the meanwhile I realized that I have ACF Plugin installed, no configuration though, but maybe these are default fields that come automatically with the installation of ACF?

    I thought that in order to have custom fields on the account tab, I need to add custom fields in UM registration form and then use some code like the one I’d been using when the problem came up with password change causing the loss of data in these fields.

    My own fields don’t show at all when I use the code you pointed out with my own meta keys as defined in my UM registration form:

    /**
     * Add custom fields to the main tab "Account" under the "E-mail Address" field.
     * Add this code to the file functions.php in the active theme directory.
     */
    function um_account_custom_fields( $args, $shortcode_args ) {
    
    	// Fields Meta Keys.
    	$args .= ',salutation';
    	$args .= ',job';
    	$args .= ',company';
    
    	return $args;
    }
    add_filter( 'um_account_tab_general_fields', 'um_account_custom_fields', 10, 2 );
    • This reply was modified 2 years, 5 months ago by celinehag.
    • This reply was modified 2 years, 5 months ago by celinehag.
    • This reply was modified 2 years, 5 months ago by celinehag.
    • This reply was modified 2 years, 5 months ago by celinehag.
    • This reply was modified 2 years, 5 months ago by celinehag.

    @celinehag

    Looking at the UM class-account.php file there is a test for UM pre-defined fields which are the only fields allowed in these account filters and custom fields are sorted out:

    um_account_tab_privacy_fields
    um_account_tab_delete_fields
    um_account_tab_general_fields
    um_account_tab_password_fields

    This code snippet adds custom fields to the main Account tab.

    The truth is that this code snippet only adds UM pre-defined fields to the Account Tab.

    https://docs.ultimatemember.com/article/1770-extend-the-account-page-with-custom-fields

    • This reply was modified 2 years, 5 months ago by missveronica.

    @celinehag

    Your first code snippet:

    Replace: update_user_meta($id, $name, $_POST[$name]);

    with: if( isset( $_POST[$name] )) update_user_meta( $id, $name, $_POST[$name] );

    Thread Starter celinehag

    (@celinehag)

    @missveronicatv

    Thanks! Your code correction works like a charm! I can now change passwords and all fields stay populated. How this is done by using isset() with update meta remains a mystery to me though.

    Thanks again for your support.

    @celinehag

    You only update when there is data in the field and not when user is updating Another account tab like password.

    @gelieys

    The code snippet in documentation needs an update of the text.

    • This reply was modified 2 years, 5 months ago by missveronica.
    Thread Starter celinehag

    (@celinehag)

    @missveronicatv

    thanks for the explanation. Now I unterstand!

    I’m closing this topic as it is resolved.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Custom field content on user account tab cleared when password is changed’ is closed to new replies.