• Resolved ninsan

    (@jennyrigsjo)


    Good afternoon!

    I have a problem that may not necessarily be caused by the UM plugin, but since it involves the UM plugin, I thought I’d post about it here.

    I have created a custom field called ‘description_brief’ (“Describe yourself in one sentence!”) in Ultimate Member > Forms > Default Profile. For the sake of consistency, I would like to also display this custom field in users’ backend profiles (wp-admin/profile.php and wp-admin/user-edit.php) so that they can easily edit the field in both places. I am using the following piece of code to display the field on the backend:

    add_action('show_user_profile', 'jr_um_admin_profile_fields');
    add_action('edit_user_profile', 'jr_um_admin_profile_fields');
    
    function jr_um_admin_profile_fields($user) {
    
    ?>
        <h2>Ultimate Member</h2>
        <table class="form-table" role="presentation">
            <tbody class="um-description-brief">
                <tr>
                    <th>
                        <label for="description_brief"><?php _e( 'I korthet' ); ?></label>
                    </th>
                    <td>
                        <input type="text" name="description_brief" id="description_brief" value="<?php echo esc_attr( get_the_author_meta( 'description_brief', $user->ID ) ); ?>" class="regular-text" />
                        <br><span class="description"><?php _e( 'Beskriv dig själv i en mening!', 'text-domain' ); ?></span>
                    </td>
                </tr>
            </tbody>
        </table>
    <?php
    }

    However, while the above code displays the field in users’s admin profiles, it doesn’t allow for the field to be updated from there. When I click the Update Profile button (either in my own admin profile or in one of my users’), nothing happens. The field value doesn’t change from the old value to the new one. What is missing here? Is it at all possible to update UM custom fields via WordPress admin?

    Any advice would be much appreciated! 🙂

    /jenny

Viewing 4 replies - 1 through 4 (of 4 total)
  • @jennyrigsjo

    You must add code to do the update and clear the UM user cache.
    See the UM profile_update function in class-user.php

    • This reply was modified 4 years, 8 months ago by missveronica.
    • This reply was modified 4 years, 8 months ago by missveronica.
    • This reply was modified 4 years, 8 months ago by missveronica.

    @jennyrigsjo

    You can add this code to your existing code:

    add_action( 'profile_update', 'jr_um_profile_update', 10, 2 ); 
    
    function jr_um_profile_update( $user_id, $old_data ) {
       
        if( isset( $_POST['description_brief'] ) ) {   
            update_user_meta( $user_id, 'description_brief', sanitize_text_field( $_POST['description_brief'] ));
            delete_option( "um_cache_userdata_{$user_id}" );        
        }
    }
    Thread Starter ninsan

    (@jennyrigsjo)

    @missveronicatv Thank you so much for helping! The code you provided worked like a charm! 🙂

    /jenny

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Thanks for letting us know how it resolved the issue.

    Regards,

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Update custom fields via wp-admin/profile.php?’ is closed to new replies.