Add multiple tabs to accounts page
-
Hello Team
I’m trying to add multiple tabs to the accounts using the sample code provided in the doc. but apparently only one tab gets added. Please help us to test this.
Also may i know what is the index used in the $tabs array e:g 9999 in $tabs[999]?
<?php
/* add new tab called "nxltab" */
add_filter('um_account_page_default_tabs_hook', 'nxl_tab_in_um', 100 );
function nxl_tab_in_um( $tabs ) {
$tabs[9999]['nxltab']['icon'] = 'um-icon-log-out';
$tabs[9999]['nxltab']['title'] = 'Logout';
$tabs[9999]['nxltab']['custom'] = true;
return $tabs;
}
/* make our new tab hookable */
add_action('um_account_tab__nxltab', 'um_account_tab__nxltab');
function um_account_tab__nxltab( $info ) {
global $ultimatemember;
extract( $info );
$output = $ultimatemember->account->get_tab_output('nxltab');
if ( $output ) { echo $output; }
}
/* Finally we add some content in the tab */
add_filter('um_account_content_hook_nxltab', 'um_account_content_hook_nxltab');
function um_account_content_hook_nxltab( $output ){
$logout_url = wp_logout_url(home_url());
ob_start();
?>
<div class="um-field">
<p>Are you sure you want to logout?</p>
<a class="um-button" href="<?php echo $logout_url; ?>">Logout</a>'
</div>
<?php
$output .= ob_get_contents();
ob_end_clean();
return $output;
}
/* Another custom tab with form */
/**
* Add custom tabs.
*
* @param array $tabs Account tabs.
* @return array
*/
function um_account_custom_tabs( $tabs ) {
$tabs[ 10000 ][ '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', 99 );
/**
* 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 );Best Regards
- You must be logged in to reply to this topic.