Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active February 28, 2020 12:26
Show Gist options
  • Save RadGH/f26e285597e63a8d60491404384784bd to your computer and use it in GitHub Desktop.
Save RadGH/f26e285597e63a8d60491404384784bd to your computer and use it in GitHub Desktop.
Add custom woocommerce account page/tab with title, content, and endpoint slug
<?php
if ( !defined( 'ABSPATH' ) ) exit;
// The "endpoint" key used to identify our custom menu.
// If you are adding multiple pages to the account menu, you would want to change this to an array or just get rid of the constant altogether..
define( 'MBC_PROFILE_ENDPOINT', 'coach-profile' );
/**
* Add the Coach Profile link to the list of menu items in the dashboard.
*
* @param $items
*
* @return array
*/
function mbc_register_coach_profile_account_menu_tab( $items ) {
// Add coach profile in the second spot in the menu items list
$items = array_splice( $items, 0, 1 ) + array( MBC_PROFILE_ENDPOINT => 'Coach Profile' ) + $items;
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'mbc_register_coach_profile_account_menu_tab', 20 );
/**
* Register the Coach Profile rewrite rule, so the URL works.
* Note: Refresh permalinks afterwards. Use flush_rewrite_rules() on plugin activation.
*/
function mbc_register_coach_profile_rewrite_rule() {
add_rewrite_endpoint( MBC_PROFILE_ENDPOINT, EP_ROOT | EP_PAGES );
}
add_filter('init', 'mbc_register_coach_profile_rewrite_rule');
/**
* Register the Coach Profile endpoint as a WooCommerce query var.
*
* @param $query_vars
*
* @return mixed
*/
function mbc_register_coach_profile_woocommerce_query_var( $query_vars ) {
$query_vars[ MBC_PROFILE_ENDPOINT ] = MBC_PROFILE_ENDPOINT;
return $query_vars;
}
add_filter( 'woocommerce_get_query_vars', 'mbc_register_coach_profile_woocommerce_query_var' );
/**
* Change the title of the Coach Profile account page.
*
* @return string
*/
function mbc_coach_profile_page_title() {
return 'Coach Profile';
}
add_filter( 'woocommerce_endpoint_'. MBC_PROFILE_ENDPOINT .'_title', 'mbc_coach_profile_page_title' );
/**
* Display content for the Coach Profile account page.
*/
function mbc_display_coach_profile_account_page_content() {
echo 'This is the page content!';
}
add_action( 'woocommerce_account_'. MBC_PROFILE_ENDPOINT .'_endpoint', 'mbc_display_coach_profile_account_page_content' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment