0

I am trying to set the title tag for Woocommerce pages that have endpoints. I am using Yoast SEO and so far have created this:

function woocommerce_endpoint_titles( $title ) {
    $sep = ' | ';
    $sitetitle = get_bloginfo();

    if ( is_wc_endpoint_url( 'view-order' ) ) {
        $title = 'View Order'. $sep . $sitetitle;   
        return $title;
    }
    else if ( is_wc_endpoint_url( 'edit-account' ) ) {
        $title = 'Edit Account'. $sep . $sitetitle; 
        return $title;
    }
    else if ( is_wc_endpoint_url( 'edit-address' ) ) {
        $title = 'Edit Address'. $sep . $sitetitle; 
        return $title;
    }
    else if ( is_wc_endpoint_url( 'lost-password' ) ) {
        $title = 'Lost Password'. $sep . $sitetitle;    
        return $title;
    }
    else if ( is_wc_endpoint_url( 'customer-logout' ) ) {
        $title = 'Logout'. $sep . $sitetitle;   
        return $title;
    }
    else if ( is_wc_endpoint_url( 'order-pay' ) ) {
        $title = 'Order Payment'. $sep . $sitetitle;    
        return $title;
    }
    else if ( is_wc_endpoint_url( 'order-received' ) ) {
        $title = 'Order Received'. $sep . $sitetitle;   
        return $title;
    }
    else if ( is_wc_endpoint_url( 'add-payment-method' ) ) {
        $title = 'Add Payment Method'. $sep . $sitetitle;   
        return $title;
    }
}
add_filter( 'wpseo_title','woocommerce_endpoint_titles');

This works, but only for the endpoint pages, all other pages now don't have a title. How can I make my above code return the default title when not on an endpoint page?

Cheers,

Steve

1

It turns out I just needed to return the $title variable which already had what I needed:

function woocommerce_endpoint_titles( $title ) {
    $sep = ' | ';
    $sitetitle = get_bloginfo();

    if ( is_wc_endpoint_url( 'view-order' ) ) {
        $title = 'View Order: ' . $sep . $sitetitle;    
    }
    if ( is_wc_endpoint_url( 'edit-account' ) ) {
        $title = 'Edit Account'. $sep . $sitetitle; 
    }
    if ( is_wc_endpoint_url( 'edit-address' ) ) {
        $title = 'Edit Address'. $sep . $sitetitle; 
    }
    if ( is_wc_endpoint_url( 'lost-password' ) ) {
        $title = 'Lost Password'. $sep . $sitetitle;    
    }
    if ( is_wc_endpoint_url( 'customer-logout' ) ) {
        $title = 'Logout'. $sep . $sitetitle;   
    }
    if ( is_wc_endpoint_url( 'order-pay' ) ) {
        $title = 'Order Payment'. $sep . $sitetitle;    
    }
    if ( is_wc_endpoint_url( 'order-received' ) ) {
        $title = 'Order Received'. $sep . $sitetitle;   
    }
    if ( is_wc_endpoint_url( 'add-payment-method' ) ) {
        $title = 'Add Payment Method'. $sep . $sitetitle;   
    }
    return $title;
}
add_filter( 'wpseo_title','woocommerce_endpoint_titles');
1

If you added custom WC endpoints on init, you can use something like this instead to check for endpoints (adjust accordingly if you're using Yoast):

function add_custom_endpoints_to_title( $post_title ) {

    if ( ! is_account_page() ) {
        return $post_title;
    }

    global $wp;

    if ( isset( $wp->query_vars['custom_endpoint_1'] ) ) {
        $post_title = 'Custom Endpoint 1';
    } elseif ( isset( $wp->query_vars['custom_endpoint_2'] ) ) {
        $post_title = 'Custom Endpoint 2';
    }

    return $post_title;
}

add_filter( 'single_post_title', 'add_custom_endpoints_to_title', 99 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.