Last active
May 17, 2025 13:20
-
-
Save yuriinalivaiko/b2984d6108946f53d7aca7a97f114278 to your computer and use it in GitHub Desktop.
Ultimate Member documentation. Article: How to display custom fields in Account.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Add custom tabs. | |
| * | |
| * @param array $tabs Account tabs. | |
| * @return array | |
| */ | |
| function um_account_custom_tabs( $tabs ) { | |
| $tabs[ 150 ][ 'custom_tab_01' ] = array( | |
| 'icon' => 'um-faicon-plus', | |
| 'title' => __( 'Custom fields', 'ultimate-member' ), | |
| 'submit_title' => __( 'Save', 'ultimate-member' ), | |
| 'custom' => true, | |
| ); | |
| return $tabs; | |
| } | |
| add_filter( 'um_account_page_default_tabs_hook', 'um_account_custom_tabs', 100 ); | |
| /** | |
| * Add custom tab content. | |
| * | |
| * @param string $output Tab content. | |
| * @return string | |
| */ | |
| function um_account_content_custom_tab_01( $output = '' ) { | |
| // List of fields you want to display. | |
| $fields = array( | |
| 'um_pet', | |
| 'um_colors', | |
| 'um_multi_select', | |
| ); | |
| ob_start(); | |
| foreach( $fields as $key ) { | |
| $data = UM()->builtin()->get_a_field( $key ); | |
| echo UM()->fields()->edit_field( $key, $data ); | |
| } | |
| return ob_get_clean(); | |
| } | |
| add_filter( 'um_account_content_hook_custom_tab_01', 'um_account_content_custom_tab_01', 20 ); | |
| /** | |
| * Validate custom fields. | |
| * | |
| * @param array $post_args Input data. | |
| */ | |
| function um_account_errors_custom_tab_01( $post_args ) { | |
| if( ! isset( $post_args[ 'um_account_submit' ] ) ) { | |
| return; | |
| } | |
| // List of fields you want to validate. | |
| $fields = array( | |
| 'um_pet', | |
| 'um_colors', | |
| 'um_multi_select', | |
| ); | |
| foreach( $fields as $key ) { | |
| $data = UM()->builtin()->get_a_field( $key ); | |
| if( empty( $post_args[ $key ] ) && ! empty( $data['required'] ) ) { | |
| UM()->form()->add_error( $key, __( 'This field is required.', 'ultimate-member' ) ); | |
| } | |
| } | |
| } | |
| add_action( 'um_submit_account_errors_hook', 'um_account_errors_custom_tab_01', 20 ); | |
| /** | |
| * Save custom fields. | |
| * | |
| * @param int $user_id User ID. | |
| */ | |
| function um_account_update_custom_tab_01( $user_id ) { | |
| // List of fields you want to update. | |
| $fields = array( | |
| 'um_pet', | |
| 'um_colors', | |
| 'um_multi_select', | |
| ); | |
| foreach( $fields as $key ) { | |
| if( isset( $_POST[ $key ] ) && ! UM()->form()->has_error( $key ) ) { | |
| update_user_meta( $user_id, $key, wp_unslash( $_POST[ $key ] ) ); | |
| } | |
| } | |
| } | |
| add_action( 'um_after_user_account_updated', 'um_account_update_custom_tab_01', 8 ); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is a part of the article How to display custom fields in Account
You can add the code to the functions.php file in the child theme directory. You also may use the Code Snippets plugin to add a code snippet.
You can use a free extension Ultimate Member - Account tabs to display custom fields in Account. This extension can embed a profile form into the custom account tab.