Last active
May 17, 2019 04:01
-
-
Save sanzeeb3/2d536212da972c86e83b72c472721f33 to your computer and use it in GitHub Desktop.
Add additional custom tab in WooCommerce myaccount page
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 | |
/** | |
* | |
* Class for adding new custom tab in WooCommerce myaccount page. | |
* | |
* @class WooCommerce_Myaccount_Tab | |
*/ | |
Class WooCommerce_Myaccount_Tab { | |
/** | |
* Constructor. | |
*/ | |
public function __construct() { | |
// Return if WooCommerce is not installed. | |
if( ! defined( 'WC_VERSION' ) ) { | |
return; | |
} | |
add_action( 'init', array( $this, 'register_endpoint' ) ); | |
add_filter( 'query_vars', array( $this, 'query_vars' ) ); | |
add_filter( 'woocommerce_account_menu_items', array( $this, 'add_new_account_tab' ) ); | |
add_action( 'woocommerce_account_new-endpoint_endpoint', array( $this, 'add_content' ) ); | |
} | |
/** | |
* Register New Endpoint. | |
* | |
* @return Void. | |
*/ | |
public function register_endpoint() { | |
add_rewrite_endpoint( 'new-endpoint', EP_ROOT | EP_PAGES ); | |
} | |
/** | |
* Add new query var. | |
* | |
* @param array $vars vars, | |
* | |
* @return array. | |
*/ | |
public function query_vars( $vars ) { | |
$vars[] = 'new-endpoint'; | |
return $vars; | |
} | |
/** | |
* Add New tab in my account page. | |
* | |
* @param array $items myaccount Items. | |
* | |
* @return array Items including New tab. | |
*/ | |
public function add_new_account_tab( $items ) { | |
$items['new-endpoint'] = 'New Item'; | |
return $items; | |
} | |
/** | |
* Add content to the new tab. | |
* | |
* @return void. | |
*/ | |
public function add_content() { | |
echo 'Contents here!'; | |
} | |
} | |
new WooCommerce_Myaccount_Tab(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment