Skip to content

Instantly share code, notes, and snippets.

@smeric
Last active June 21, 2021 22:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smeric/b544f154ea752e47d0427f3e1ae901b5 to your computer and use it in GitHub Desktop.
Save smeric/b544f154ea752e47d0427f3e1ae901b5 to your computer and use it in GitHub Desktop.
Add WooCommerce endpoints to Yoast SEO breadcrumbs
<?php
/**
* Add WooCommerce endpoints to Yoast SEO breadcrumbs
*
* @link https://gist.github.com/smeric/b544f154ea752e47d0427f3e1ae901b5
* @version 1.0.1
* @package yoast_seo_breadcrumbs_for_woocommerce_endpoints
*
* @wordpress-plugin
* Plugin Name: Yoast SEO breadcrumbs addon
* Plugin URI: https://gist.github.com/smeric/b544f154ea752e47d0427f3e1ae901b5
* Description: Add WooCommerce endpoints to Yoast SEO breadcrumbs.
* Version: 1.0.1
* Author: Sébastien Méric <sebastien.meric@gmail.com>
* Author URI: http://www.sebastien-meric.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: yoast_seo_breadcrumbs_for_woocommerce_endpoints
* Domain Path: /languages
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Add WooCommerce endpoints to Yoast SEO breadcrumbs.
*
* @since 1.0.0
* @package yoast_seo_breadcrumbs_for_woocommerce_endpoints
* @author Sébastien Méric <sebastien.meric@gmail.com>
**/
add_filter( 'wpseo_breadcrumb_links', 'my_prefix_override_yoast_breadcrumb_trail' );
function my_prefix_override_yoast_breadcrumb_trail( $links ) {
if ( function_exists( 'is_wc_endpoint_url' ) && is_wc_endpoint_url() ) {
// Remove the last element : "My account" unlinked title...
array_pop( $links );
// ... And replace it with the linked one
$links[] = array(
'url' => get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ),
'text' => get_the_title( get_option( 'woocommerce_myaccount_page_id' ) ),
);
// Get endpoints list
$endpoints = WC()->query->get_query_vars();
// For each endpoint, add its title at breadcrumbs end
foreach ( $endpoints as $endpoint => $translated_endpoint ) {
if ( is_wc_endpoint_url( $endpoint ) ) {
// Special case for addresses editon pages
if ( $endpoint === 'edit-address' ) {
global $wp;
$load_address = isset( $wp->query_vars['edit-address'] ) ? wc_edit_address_i18n( sanitize_title( $wp->query_vars['edit-address'] ), true ) : 'billing';
if ( 'billing' === $load_address ) {
// For billing address editon page
$links[] = array(
'url' => wc_get_endpoint_url( $endpoint ),
'text' => WC()->query->get_endpoint_title( $endpoint ),
);
$links[] = array(
'url' => '',
'text' => __( 'Billing Address', 'woocommerce' ),
);
break;
}
elseif ( 'shipping' === $load_address ) {
// For shipping address editon page
$links[] = array(
'url' => wc_get_endpoint_url( $endpoint ),
'text' => WC()->query->get_endpoint_title( $endpoint ),
);
$links[] = array(
'url' => '',
'text' => __( 'Shipping Address', 'woocommerce' ),
);
break;
}
else {
// For addresses editon page "menu"
$links[] = array(
'url' => '',
'text' => WC()->query->get_endpoint_title( $endpoint ),
);
break;
}
}
// Default case : add current endpoint title with no link at breadcrumbs end
else {
$links[] = array(
'url' => '',
'text' => WC()->query->get_endpoint_title( $endpoint ),
);
break;
}
}
}
}
// We are on a "my account" page but not on a classical registered endpoint
elseif ( is_account_page() && ! is_wc_endpoint_url() ) {
// Deal with Iconic way of doing things...
// See https://iconicwp.com/products/woocommerce-account-pages/
if ( class_exists( 'Iconic_WAP_Pages' ) && $endpoint = Iconic_WAP_Pages::is_endpoint() ) {
$links[] = array(
'url' => '',
'text' => get_the_title( $endpoint ),
);
}
// We are on the root
else {
$links[] = array(
'url' => '',
'text' => __( 'Dashboard', 'woocommerce' ),
);
}
}
return $links;
}
/**
* Register unregistered endpoints...
*
* @since 1.0.1
* @package yoast_seo_breadcrumbs_for_woocommerce_endpoints
* @author Sébastien Méric <sebastien.meric@gmail.com>
**/
add_filter( 'woocommerce_get_query_vars', 'my_prefix_register_specific_endpoints' );
function my_prefix_register_specific_endpoints( $vars ) {
/*
* WooCommerce Smart Coupon
*/
// Get the endpoint slug as defined in WooCommerce > Settings > Advanced
$smart_coupon_endpoint = get_option( 'woocommerce_myaccount_wc_sc_dashboard_endpoint', 'wc-smart-coupons' );
// Add it to query_vars so the page responds to is_wc_endpoint_url function
$vars[$smart_coupon_endpoint] = $smart_coupon_endpoint;
// Give this endpoint a title
add_filter( 'woocommerce_endpoint_' . $smart_coupon_endpoint . '_title', function(){
return __( 'Coupons', 'yoast_seo_breadcrumbs_for_woocommerce_endpoints' );
});
return $vars;
}
?>
@diego-quintero
Copy link

@smeric thanks for sharing this code. It is very helpful. I sent you an email, let me know if you got it.
Bests!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment