افزودن endpoint جدید توی پنل کابری ووکامرس
داشتم روی یه پروژه وردپرسی کار که میگردم از ووکامرس توی پروژه استفاه شده . دنبال یه متدی بودم که به صفحه my-account یه سری endpoint جدید ضافه کنم .توی گیت هاب این کلاس رو دیدم. :
ولی یه ایرادی که داشت کل کلاس رو برای یه اندپوینت نوشته بود با تغییرات جزیی اینطوری شد:
<?phpclass CMyAccountEP {
/*** Custom endpoint name.** @var string*/public $endpoint = '';
public $endpoinTitle='';
/*** Plugin actions.*/public function __construct($epname,$eptitle='') {
// Actions used to insert a new endpoint in the WordPress.$this->endpoint=$epname;
$this->endpoinTitle=$eptitle;
add_action( 'init', array( $this, 'add_endpoints' ) );
add_filter( 'woocommerce_get_query_vars', array( $this, 'get_query_vars' ), 0 );
// Change the My Accout page title.add_filter( 'the_title', array( $this, 'endpoint_title' ) );
// Insering your new tab/page into the My Account page.add_filter( 'woocommerce_account_menu_items', array( $this, 'new_menu_items' ) );
add_action( 'woocommerce_account_' . $this->endpoint . '_endpoint', array( $this, 'endpoint_content' ) );
}/*** Register new endpoint to use inside My Account page.** @see https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/*/public function add_endpoints() {
add_rewrite_endpoint( $this->endpoint, EP_ROOT | EP_PAGES );
}/*** Add new query var.** @param array $vars* @return array*/public function get_query_vars( $vars ) {
$vars[ $this->endpoint ] = $this->endpoint;
return $vars;
}/*** Set endpoint title.** @param string $title* @return string*/public function endpoint_title( $title ) {
global $wp_query;
$is_endpoint = isset( $wp_query->query_vars[ $this->endpoint ] );
if ( $is_endpoint && ! is_admin() && is_main_query() && in_the_loop() && is_account_page() ) {
// New page title.$title = __( $this->endpoinTitle, 'woocommerce' );
remove_filter( 'the_title', array( $this, 'endpoint_title' ) );
}return $title;
}/*** Insert the new endpoint into the My Account menu.** @param array $items* @return array*/public function new_menu_items( $items ) {
// Remove the logout menu item.$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
// Insert your custom endpoint.$items[ $this->endpoint ] = __( $this->endpoinTitle, 'woocommerce' );
// Insert back the logout item.$items['customer-logout'] = $logout;
return $items;
}/*** Endpoint HTML content.*/public function endpoint_content() {
wc_get_template( 'myaccount/'.$this->endpoint.'.php' );
}/*** Plugin install action.* Flush rewrite rules to make our custom endpoint available.*/public static function install() {
flush_rewrite_rules();
}}new CMyAccountEP('my-ex-ep','my Example Ep');
new CMyAccountEP('another-ep','Another Ep');
// Flush rewrite rules on plugin activation.//register_activation_hook( __FILE__, array( 'CMyAccountEP', 'install' ) );// Run Flush in theme functionadd_action( 'after_switch_theme', array( 'CMyAccountEP', 'install' ) );
الان با اضافه کردن این فایل توی فانکشن یا توی پلاگینتون میتونین دوتا اند پوینت جدید به ادرسای زیر داشته باشید:
yoururl.com/my-acount/my-ex-ep
yoururl.com/my-acount/another-ep
توجه کنید که برای نشان دادن محتوای این صفحات نیازه دوتا فایل php توی پوشه my-account تمپلیت ووکامرس به همین نام ها( my-ex-ep.php, another-ep.php )داشته باشد…
دیدگاهتان را بنویسید