Reputation: 1473
I am working with woo-commerce user dashboard template.
I need to print the title of current endpoint instead of the_title();
.
following image snapshot of
DOMIAN.com/my-account/orders/. there should be page title as "My Order" but it's "My Account".
Same requirement for other endpoint titles too.
Please help me out.
Upvotes: 2
Views: 8881
Reputation: 33
I know this outdated but this code below has been tested and works...
function wc_custom_end_point_page_title( $title ) {
global $wp_query;
$is_endpoint = isset( $wp_query->query_vars['my-custom-endpoint'] );
if ( $is_endpoint && ! is_admin() && is_main_query() && in_the_loop() && is_account_page() ) {
$title = __( 'Custom Endpoint Title', 'woocommerce' );
remove_filter( 'the_title', 'wc_custom_end_point_page_title' );
}
return $title;
}
add_filter( 'the_title', 'wc_custom_end_point_page_title' );
Upvotes: 0
Reputation: 514
<?php
add_filter( 'the_title', 'wc_page_endpoint_title' );
the_title( '<h1 class="page-title mb-4">', '</h1>' );
?>
/**
* Generates account page titles
*
* @see wc_page_endpoint_title
*
* @param string $title Current title.
* @return string
*/
function vralle_generate_wc_endpoint_title( string $title ): string {
$action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';
$endpoint = WC()->query->get_current_endpoint();
$ep_title = $endpoint ? WC()->query->get_endpoint_title( $endpoint, $action ) : '';
if ( ! empty( $ep_title ) ) {
return $ep_title;
}
return $title;
}
/**
* Filters document title.
*
* @param array $title The document title parts.
*
* @return array
*/
function vralle_document_title( $title ) {
if ( is_page() && function_exists( 'is_wc_endpoint_url' ) && is_wc_endpoint_url() ) {
$title['title'] = vralle_generate_wc_endpoint_title( $title['title'] );
}
return $title;
}
add_filter( 'document_title_parts', 'vralle_document_title );
Upvotes: 6
Reputation: 356
The original order of the My Account menu items can be seen in /wp-content/plugins/woocommerce/includes/wc-account-functions.php
/**
* Get My Account menu items.
*
* @since 2.6.0
* @return array
*/
function wc_get_account_menu_items() {
return apply_filters( 'woocommerce_account_menu_items', array(
'dashboard' => __( 'Dashboard', 'woocommerce' ),
'orders' => __( 'Orders', 'woocommerce' ),
'downloads' => __( 'Downloads', 'woocommerce' ),
'edit-address' => __( 'Addresses', 'woocommerce' ),
'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
'edit-account' => __( 'Account Details', 'woocommerce' ),
'customer-logout' => __( 'Logout', 'woocommerce' ),
) );
}
You can change the order of these endpoints by using the woocommerce_account_menu_items filter, you can also change the list item title with the same filter.
<?php
function wpb_woo_my_account_order() {
$myorder = array(
'my-custom-endpoint' => __( 'My Stuff', 'woocommerce' ),
'edit-account' => __( 'Change My Details', 'woocommerce' ),
'dashboard' => __( 'Dashboard', 'woocommerce' ),
'orders' => __( 'Orders', 'woocommerce' ),
'downloads' => __( 'Download MP4s', 'woocommerce' ),
'edit-address' => __( 'Addresses', 'woocommerce' ),
'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
'customer-logout' => __( 'Logout', 'woocommerce' ),
);
return $myorder;
}
add_filter ( 'woocommerce_account_menu_items', 'wpb_woo_my_account_order' );
One of the limitations with changing the list item title is that it won’t change the entry-title.
One way around changing the entry-title of the WooCommerce custom endpoint is to use the_title filter with the in_the_loop conditional.
<?php
/*
* Change the entry title of the endpoints that appear in My Account Page - WooCommerce 2.6
* Using the_title filter
*/
function wpb_woo_endpoint_title( $title, $id ) {
if ( is_wc_endpoint_url( 'downloads' ) && in_the_loop() ) { // add your endpoint urls
$title = "Download MP3s"; // change your entry-title
}
elseif ( is_wc_endpoint_url( 'orders' ) && in_the_loop() ) {
$title = "My Orders";
}
elseif ( is_wc_endpoint_url( 'edit-account' ) && in_the_loop() ) {
$title = "Change My Details";
}
return $title;
}
add_filter( 'the_title', 'wpb_woo_endpoint_title', 10, 2 );
Upvotes: 12