1

I want to change the page titles (entry titles) for each page in the WooCommerce My Account section so that they relate to the actual page you are on rather than outputting a generic "My Account" on each page.

I have looked around and seen this solution in several places:

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 );

This does not work unless you remove the in_the_loop check, which obviously isn't ideal as then it ends up changing other things on the page too.

Then I found this answer, as an example on how to change the title for the "Account Details" page:

add_filter( 'woocommerce_endpoint_edit-account_title', 'change_my_account_edit_account_title', 10, 2 );
function change_my_account_edit_account_title( $title, $endpoint ) {
    $title = __( "Edit your account details", "woocommerce" );

    return $title;
}

But this didn't work, it didn't even seem to go into the function at all.

Is there a way to do this that actually works?

Brett
  • 19,449
  • 54
  • 157
  • 290

3 Answers3

6

Changing main my account page title and My account page title sub sections:

1) For "My Account: dashbord (main page):

To change the main "My account" page title, you just need to change the title of the page in backend directly because it's not an endpoint, and to check in Settings > Advanced section that the page (with the renamed title) is still assigned to "My account page".

enter image description here


enter image description here


2) For the other "endpoints" page titles in My account section:

Use woocommerce_endpoint_{$endpoint}_title composite dedicated filter hook, where {$endpoint} need to be replaced by the targeted endpoint (see available endpoints on Settings > Advanced section)

Example: To change My account "orders" endpoint page title you will use:

add_filter( 'woocommerce_endpoint_orders_title', 'change_my_account_orders_title', 10, 2 );
function change_my_account_orders_title( $title, $endpoint ) {
    $title = __( "Your orders", "woocommerce" );

    return $title;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

If it doesn't work, it's because something else is making trouble, like your own customizations, your theme's customizations, a plugin or something else…


Related StackOverFlow answers:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • For some reason this isn't working for me; I will have to investigate further. – Brett Feb 27 '19 at 22:39
  • @Brett sorry but if you change the page title of "my account" page, it changes the title, as it's a page, so the default displayed title for "dashbord" (that is not an endpoint). Same thing for cart, checkout and Terms and conditions… – LoicTheAztec Feb 27 '19 at 22:43
  • @LoicTheAztec Maybe he needs to rethink his question. – Mr. Jo Feb 27 '19 at 22:49
  • 1
    @Mr.Jo He want to change the page title, not the menu items label names… My answer works, but not for him and I don't know why… – LoicTheAztec Feb 27 '19 at 22:57
  • 1
    @LoicTheAztec Hmm so thats his problem. We can't see thru our magic glass sphere into his child theme. – Mr. Jo Feb 27 '19 at 22:58
  • @LoicTheAztec I know "my account" is a page, that is fine if you're on the dashboard, but I don't want it saying "My Account" when you're on the orders endpoint or any of the other endpoints, I want the title to reflect the endpoint. – Brett Feb 27 '19 at 23:04
  • 1
    @Brett I've did the exact same thing on my page with the function the_title. If Loic's or my answer don't worked on you page, there must be something dramatically wrong with your page. – Mr. Jo Feb 27 '19 at 23:06
  • I had the same problem with the filter not firing. That's because I added a new endpoint and is_wc_endpoint_url() returned false which in turn meant the filters were not triggered (see wc_page_endpoint_title() for the code). Solution was to add my endpoint as query vars. Note: the query_vars must contain the endpoint as key: ```add_filter( 'woocommerce_get_query_vars', 'modify_woocommerce_query_vars' ); function modify_woocommerce_query_vars( $vars ) { $vars['my-endpoint'] = 'my-endpoint'; return $vars; }``` – E Allison Dec 02 '22 at 16:13
3

I notice I had some issues implementing the is_wc_endpoint_url() && in_the_loop solution, it did not work out with the && in_the_loop(), removing is not a viable option since it created even more conflicts.

So instead I chose to work on the template file directly, I am sharing this because I noticed that some people had the same issues and this solution might help others achieve the similar results using a different approach.

I basically replaced the header in page-my-account.php with this code:

<header>
    <?php
        $main_title = get_the_title();
        
        $endpoint = WC()->query->get_current_endpoint();  
        $endpoint_title = __( WC()->query->get_endpoint_title( $endpoint ), 'text-domain' );  
        
        if ( $endpoint_title ){
            //If the endpoint exists use itself as the new custom title
            $custom_title = $endpoint_title;
        }
        else{
            // Here we can define the custom title for each endpoint we can use home_url() or strpos + add_query_arg()
            global $wp;
            if( basename( home_url($wp->request) ) == 'points-and-rewards' || ( strpos( (add_query_arg( $wp->query_vars)) , 'points-and-rewards' ) !== false ) ){
                $custom_title = __( 'Points and rewards', 'text-domain' );
            }
            elseif( basename(home_url($wp->request)) == 'payment-methods' ){
                $custom_title = __( 'Payment methods', 'text-domain' );
            }
            elseif( basename(home_url($wp->request)) == 'my-account' ){
                $custom_title = __( 'Dashboard', 'text-domain' );
            }
            //Add more custom titles here
            
        }

        if ( !empty( $custom_title ) ){
            //If there is a custom title
            $new_endpoint_title =  sprintf( '<h2>%s > %s</h2>', $main_title, $custom_title );
        }
        else{
            //If there is no custom title default to get_title()
            $new_endpoint_title = sprintf( '<h2>%s</h2>' , $main_title );
        }
    ?> 
    
    <?php //Echo's the resulting title ?>
    <?php echo $new_endpoint_title; ?>

</header>

  • Thank you, this helped me. I had the same issues some of the others expressed where `&& in_the_loop()` did not work for me and without it, the titles of my menu items in my main nav and in my footer were also changed. I believe it was an issue with how my theme set the page titles in the page header and in the `` tag. Your suggestion helped me to resolve it. – bowlerae Jun 27 '23 at 19:24
2

You can change you MyAccount item titles this way:

/**
 * Rename WooCommerce MyAccount menu items
 */
add_filter( 'woocommerce_account_menu_items', 'rename_menu_items' );
function rename_menu_items( $items ) {

    $items['downloads']    = 'Download MP3s';
    $items['orders']       = 'My Orders';
    $items['edit-account'] = 'Change My Details';

    return $items;
}

To change the title on each account page as well, you need to add this too:

/**
 * Change page titles
 */
add_filter( 'the_title', 'custom_account_endpoint_titles' );
function custom_account_endpoint_titles( $title ) {
    global $wp_query;

    if ( isset( $wp_query->query_vars['downloads'] ) && in_the_loop() ) {
        return 'Download MP3s';
    }

    if ( isset( $wp_query->query_vars['orders'] ) && in_the_loop() ) {
        return 'My Orders';
    }

    if ( isset( $wp_query->query_vars['edit-account'] ) && in_the_loop() ) {
        return 'Change My Details';
    }

    return $title;
}

If you're using Yoast SEO, you need to add another function to set the correct page titles in the browser tab. If you also need this, I'll expand my answer.

Put this into you functions.php file. Tested and works.

Mr. Jo
  • 4,946
  • 6
  • 41
  • 100
  • According to [this page](https://wpbeaches.com/change-rename-woocommerce-endpoints-accounts-page/) that doesn't change the entry title. – Brett Feb 27 '19 at 22:36
  • 1
    The OP doesn't want to change the menu item titles, but the page titles :) – LoicTheAztec Feb 27 '19 at 22:40
  • @Brett Tried it? – Mr. Jo Feb 27 '19 at 22:44
  • You are posting the exact same code that I said didn't work? – Brett Feb 27 '19 at 23:03
  • @Brett You've posted the woocommerce_endpoint_orders_title hook. I'm using an other hook here... So this is not the same hook. But you must know it ;) – Mr. Jo Feb 27 '19 at 23:07
  • Please see the first block of code: `add_filter( 'the_title', 'wpb_woo_endpoint_title', 10, 2 );` - I _am_ using `the_title` hook. – Brett Feb 28 '19 at 09:18