Created
April 29, 2023 14:17
-
-
Save vulieumang/ba1e7fcc21c4db3406bddb99382eb8a5 to your computer and use it in GitHub Desktop.
// Add custom tab in ultimate member account page.
This file contains 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
// Add custom tab in ultimate member account page. | |
// global $form_id; | |
// $form_id = 26; | |
/** | |
* 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', 'um-woocommerce' ), | |
'submit_title' => __( 'Save', 'um-woocommerce' ), | |
'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 = get_all_custom_fields('26'); | |
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 = get_all_custom_fields('26'); | |
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 = get_all_custom_fields('26'); | |
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 ); | |
// remove element in array if type equal number | |
function remove_element_by_type($array, $type) { | |
foreach ($array as $key => $value) { | |
if ($value['type'] == $type) { | |
unset($array[$key]); | |
} | |
} | |
return $array; | |
} | |
// get all custom fields | |
function get_all_custom_fields($form_id = null) { | |
$custom_fields = UM()->query()->get_attr( 'custom_fields', $form_id ); | |
$custom_fields = remove_element_by_type($custom_fields, 'row'); | |
// get all key in array | |
$custom_fields = array_keys($custom_fields); | |
return $custom_fields; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment