Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save diego-quintero/3916487f594379e2724ad88d1d22e17a to your computer and use it in GitHub Desktop.
Save diego-quintero/3916487f594379e2724ad88d1d22e17a 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.0
* @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.0
* Author: Sébastien Méric <[email protected]>
* 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 woocommerce_add_to_cart_before_applying_coupon
* @author Sébastien Méric <[email protected]>
**/
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 las 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_page_endpoint_title( __(ucfirst(str_replace('-',' ',$translated_endpoint)), 'woocommerce' ) ),
);
$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_page_endpoint_title( __(ucfirst(str_replace('-',' ',$translated_endpoint)), 'woocommerce' ) ),
);
$links[] = array(
'url' => '',
'text' => __( 'Shipping Address', 'woocommerce' ),
);
break;
}
else {
// For addresses editon page "menu"
$links[] = array(
'url' => '',
'text' => wc_page_endpoint_title( __(ucfirst(str_replace('-',' ',$translated_endpoint)), 'woocommerce' ) ),
);
break;
}
}
// Default case : add current endpoint title with no link at breadcrumbs end
else {
$links[] = array(
'url' => '',
'text' => wc_page_endpoint_title( __(ucfirst(str_replace('-',' ',$translated_endpoint)), 'woocommerce' ) ),
);
break;
}
}
}
}
return $links;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment