Skip to content

Instantly share code, notes, and snippets.

@amboutwe
Last active August 23, 2024 01:58
Show Gist options
  • Save amboutwe/ea0791e184668a5c7bd7bbe357c598e9 to your computer and use it in GitHub Desktop.
Save amboutwe/ea0791e184668a5c7bd7bbe357c598e9 to your computer and use it in GitHub Desktop.
Multiple examples of how to customize the Yoast SEO breadcrumbs
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/*
* Add shop link to the Yoast SEO breadcrumbs for a WooCommerce shop page.
* Credit: https://wordpress.stackexchange.com/users/8495/rjb
* Last Tested: Apr 20 2017 using Yoast SEO 4.6 on WordPress 4.7.3
*/
add_filter( 'wpseo_breadcrumb_links', 'wpseo_breadcrumb_add_woo_shop_link' );
function wpseo_breadcrumb_add_woo_shop_link( $links ) {
global $post;
if ( is_woocommerce() ) {
$breadcrumb[] = array(
'url' => get_permalink( woocommerce_get_page_id( 'shop' ) ),
'text' => 'Shop',
);
array_splice( $links, 1, -2, $breadcrumb );
}
return $links;
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/*
* Remove breadcrumbs for specific items
* Credit: Yoast team
* Last Tested: Jul 21 2022 using Yoast SEO 19.3 on WordPress 6.0.1
*********
* DIFFERENT POST TYPES
* Post: Change 123456 to the post ID
* Page: Change is_single to is_page and 123456 to the page ID
* Custom Post Type: Change is_single to is_singular and 123456 to the 'post_type_slug'
Example: is_singular( 'cpt_slug' )
*********
* MULTIPLE ITEMS
* Multiple of the same type can use an array.
Example: is_single( array( 123456, 234567, 345678 ) )
* Multiple of different types can repeat the if statement
*/
add_filter( 'wpseo_breadcrumb_links', 'wpseo_breadcrumb_remove_limited' );
function wpseo_breadcrumb_remove_limited( $breadcrumbs ) {
if ( is_single ( 123456 ) ) {
return array(
'url' => '',
'text' => ''
);
}
else {
return $breadcrumbs;
}
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Overwrite Rank Math breadcrumb shortcode with Yoast breadcrumbs
* Credit: Yoast team
* Last Tested: April 10, 2023 using Yoast SEO 20.4 on WordPress 6.2
* Rank Math must be inactive to overwrite the shortcode
*/
add_action('init', 'overwrite_rankmath_bc_shortcode_with_Yoast');
function overwrite_rankmath_bc_shortcode_with_Yoast() {
add_shortcode('rank_math_breadcrumb', 'my_custom_wpseo_breadcrumb');
}
function my_custom_wpseo_breadcrumb() {
return do_shortcode('[wpseo_breadcrumb]');
}
@vivekkotadiya
Copy link

vivekkotadiya commented Jul 29, 2022

Why this filter hook is not worked with woocommerce product category?
Here is my code:

add_filter( 'wpseo_breadcrumb_links', 'wpse_100012_override_yoast_breadcrumb_trail' );

function wpse_100012_override_yoast_breadcrumb_trail( $links ) {
    global $post;

	if( is_product_category() ) {
		$breadcrumb[] = array(
			'url' => get_permalink( 'my-category-custom-prefix' ),
			'text' => 'MY_CATEGORY_CUSTOM_PREFIX'
		);

		array_splice( $links, 0, -1, $breadcrumb );
	}

    return $links;
}

@yes-or-not
Copy link

Hi
I have shop created in WP in subpage "Shop" not on main page. How to change link from 'home' in breadcrumbs to "shop page" ?
To be clear: there is page in domain "www.mydomain.xx" and shop on page "www.mydomain.xx/shop" and when I am on page with shop or products I want to have breadcrumbs linking to main shop page so to "www.mydomain.xx/shop" not like it is now to www.mydomain.xx How to do this? I want to have home page=shop page not main domain page.....

@vivekkotadiya
Copy link

@yes-or-not I think you can do it by go to Setting > Reading > Set shop page as a home page if you want to set shop page as a main landing page.

Hope this will work for you.

Thank you. Regards,
Vivek

@yes-or-not
Copy link

vivekkotadiya
Probably yes but I want to have 'shop' as main only if I am on sub-pages of shop and products. There are many pages not connected with shop....

@vivekkotadiya
Copy link

@yes-or-not Is it possible to share your URL for explain how you want you URL structure for main page as well as sub pages.

@yes-or-not
Copy link

sure but it isn't going to be easy for you sice it is not in english.
www.tomauto.pl and www.tomauto.pl/sklep

@vivekkotadiya
Copy link

No issues I will catch it. Now let me know which URL should be your main landing page URL?

@yes-or-not
Copy link

All related with shop should go to tomauto.pl/sklep (main shop page and categories and products) Rest should go to main domain page.

@vivekkotadiya
Copy link

Have you used any SEO plugin...? LIke yoast or All in one SEO? Or how you have set breadcrumb on your site?

@yes-or-not
Copy link

yes-or-not commented Sep 20, 2022

yes, i have yoast SEO plugin but did not change any settings related with breadcrumbs. "BRAK" means = "no / blank"
image

@vivekkotadiya
Copy link

vivekkotadiya commented Sep 20, 2022

No need to change anything here. There is one small filter hook for that.

add_filter( 'wpseo_breadcrumb_links', 'dgtlnk_breadcrumbs' );

function dgtlnk_breadcrumbs( $links ) {
    global $post;
    if ( is_woocoomerce() ) {
        $breadcrumb[] = array(
            'url' => woocoommerce_shop_page(),
            'text' => 'Shop',
        );

        array_splice( $links, 1, -2, $breadcrumb );
    }    
    return $links;
}

This will work I think. In my case there was issue with product category page.

@yes-or-not
Copy link

Is there any mistake in the code?
I put this code to functions.php in my theme but I got critical error when refresh my site.

@vivekkotadiya
Copy link

vivekkotadiya commented Sep 20, 2022

add_filter( 'wpseo_breadcrumb_links', 'fs_breadcrumbs' );

function fs_breadcrumbs( $links ) {
global $post;
if ( is_woocommerce() ) {
$breadcrumb[] = array(
'url' => wc_get_page_permalink( 'shop' ),
'text' => 'Shop',
);

    array_splice( $links, 1, -2, $breadcrumb );
}    
return $links;
}

Try this, this will not break. Tested snippet.

Thank you. Regards,
Vivek

@yes-or-not
Copy link

ok there is not error any more but I could not see any changes in breadcrumbs:
image
there should not be "main page" at the begging of breadcrumbs . Should link to 'sklep' means "shop" which still is on second position.

@vivekkotadiya
Copy link

vivekkotadiya commented Sep 20, 2022

Okay so for these pages you want to remove first main page URL right...? If so then try with this updated code:

add_filter( 'wpseo_breadcrumb_links', 'fs_breadcrumbs' );

function fs_breadcrumbs( $links ) {
global $post;
if ( is_woocommerce() ) {
$breadcrumb[] = array(
'url' => wc_get_page_permalink( 'shop' ),
'text' => 'Shop',
);

array_splice( $links, 1, -2, $breadcrumb );

unset($links[0]);
}
return $links;
}

Hope this worked!

Thank you. Regards,
Vivek
w: https://flexsolutions.co.in/

@yes-or-not
Copy link

yes-or-not commented Sep 20, 2022

no, any results in breakcrums. Still the same look. Maybe should I change something in Yoast SOE plugin? I have marked checkbox next to enable breadcrumbs in choosen theme. What else can be wrong ?

@vivekkotadiya
Copy link

@yes-or-not Sorry to missed to reply here. Is it possible to share your admin login or staging enviornment so I can look into it and help you to fix it.

@yes-or-not
Copy link

yes-or-not commented Sep 26, 2022

Sorry but I can't give you login data.
Here is code from "functions" file:

<?php
/**
*
* This program is a free software; you can use 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( 'CZR_MIN_PHP_VERSION' ) ) define ( 'CZR_MIN_PHP_VERSION', 5.3 );
if ( !defined( 'CZR_MIN_WP_VERSION' ) ) define ( 'CZR_MIN_WP_VERSION', 4.5 );

if ( version_compare( phpversion(), CZR_MIN_PHP_VERSION, '<' ) ) {
    add_action( 'admin_notices'             , 'czr_fn_display_min_php_message' );
    return;
}
global $wp_version;
if ( version_compare( $wp_version, CZR_MIN_WP_VERSION, '<' ) ) {
    add_action( 'admin_notices'             , 'czr_fn_display_min_wp_message' );
    return;
}

function czr_fn_display_min_php_message() {
    czr_fn_display_min_requirement_notice( __( 'PHP', 'customizr' ), CZR_MIN_PHP_VERSION );
}

function czr_fn_display_min_wp_message() {
    czr_fn_display_min_requirement_notice( __( 'WordPress', 'customizr' ), CZR_MIN_WP_VERSION );
}


function czr_fn_display_min_requirement_notice( $requires_what, $requires_what_version ) {
    $theme = wp_get_theme()->Name;
    printf( '<div class="error"><p>%1$s</p></div>',
        sprintf( __( 'The <strong>%1$s</strong> theme requires at least %2$s version %3$s', 'customizr' ),
            $theme,
            $requires_what,
            $requires_what_version
        )
    );
}

/**
* This is where Customizr starts. This file defines and loads the theme's components :
* => Constants : CUSTOMIZR_VER, TC_BASE, TC_BASE_CHILD, TC_BASE_URL, TC_BASE_URL_CHILD, THEMENAME, CZR_WEBSITE
* => Default filtered values : images sizes, skins, featured pages, social networks, widgets, post list layout
* => Text Domain
* => Theme supports : editor style, automatic-feed-links, post formats, navigation menu, post-thumbnails, retina support
* => Plugins compatibility : JetPack, bbPress, qTranslate, WooCommerce and more to come
* => Default filtered options for the customizer
* => Customizr theme's hooks API : front end components are rendered with action and filter hooks
*
* The method CZR__::czr_fn__() loads the php files and instantiates all theme's classes.
* All classes files (except the class__.php file which loads the other) are named with the following convention : class-[group]-[class_name].php
*
* The theme is entirely built on an extensible filter and action hooks API, which makes customizations easy and safe, without ever needing to modify the core structure.
* Customizr's code acts like a collection of plugins that can be enabled, disabled or extended.
*
* If you're not familiar with the WordPress hooks concept, you might want to read those guides :
* http://docs.presscustomizr.com/article/26-wordpress-actions-filters-and-hooks-a-guide-for-non-developers
* https://codex.wordpress.org/Plugin_API
*/

//Fire Customizr
require_once( get_template_directory() . '/core/init-base.php' );


/**
* THE BEST AND SAFEST WAY TO EXTEND THE CUSTOMIZR THEME WITH YOUR OWN CUSTOM CODE IS TO CREATE A CHILD THEME.
* You can add code here but it will be lost on upgrade. If you use a child theme, you are safe!
*
* Don't know what a child theme is ? Then you really want to spend 5 minutes learning how to use child themes in WordPress, you won't regret it :) !
* https://codex.wordpress.org/Child_Themes
*
* More informations about how to create a child theme with Customizr : http://docs.presscustomizr.com/article/24-creating-a-child-theme-for-customizr/
* A good starting point to customize the Customizr theme : http://docs.presscustomizr.com/article/35-how-to-customize-the-customizr-wordpress-theme/
*/
/**
 * Filter payment gateways
 */
function my_custom_available_payment_gateways( $gateways ) {
	$chosen_shipping_rates = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();

		if ( in_array( 'flat_rate:4', $chosen_shipping_rates ) ) :
		unset( $gateways['bacs'],$gateways['wppay'],$gateways['wppay-blik-zero'], $gateways['wppay-card']);
		
		elseif ( in_array( 'flat_rate:3', $chosen_shipping_rates ) ) :
		unset( $gateways['cod'] );
		
		elseif ( in_array( 'flat_rate:5', $chosen_shipping_rates ) ) :
		unset( $gateways['cod'] );
		
		elseif ( in_array( 'local_pickup:2', $chosen_shipping_rates ) ) :
		unset( $gateways['bacs'] );

	endif;
	return $gateways;
}

add_filter( 'wpseo_breadcrumb_links', 'fs_breadcrumbs' );

function fs_breadcrumbs( $links ) {
global $post;
if ( is_woocommerce() ) {
$breadcrumb[] = array(
'url' => wc_get_page_permalink( 'sklep' ),
'text' => 'Sklep',
);

array_splice( $links, 1, -2, $breadcrumb );

unset($links[0]);
}
return $links;
}

I changed only text "shop" into "sklep".

@vivekkotadiya
Copy link

vivekkotadiya commented Sep 26, 2022

@yes-or-not is it possible to put the following before unset($links[0]);?

echo '<pre>';
print_r($links);
die;

If possible put this and see what output returns.

@yes-or-not
Copy link

where should I put this commend?

@mmediax
Copy link

mmediax commented Jan 23, 2023

Hi, thanks for these.

Do you know how to add 2 links to breadcrumbs´s path instead just one?

Home » Link 1 » Link 2 » Single post

And how to remove Home link with Yoast Seo?

Link 1 » Link 2 » Single post

Thank you!

@amboutwe
Copy link
Author

This is not the proper place to request support. Please check out our extensive help section or visit the free support forum. If you require further support, upgrading to our premium version provides you with access to our support team.

@yhlee-op
Copy link

yhlee-op commented May 14, 2023

for people who need to update the url for the categories

// EDITING CATEGORY URL
add_filter( 'wpseo_breadcrumb_links', 'custom_breadcrumb_links' );

function custom_breadcrumb_links( $links ) {
    // Loop through the breadcrumb links array
    foreach ( $links as $index => $link ) {
        // Check if this is the CATEGORY
        if ( $link['text'] === 'Blog' ) {
            // Set the custom URL for breadcrumb
            $custom_url = 'https://example.com/blog';

            // Modify the link array with the custom URL
            $links[$index]['url'] = $custom_url;
			
        } elseif ( $link['text'] === 'Installers' ) {
			// Set the custom URL for breadcrumb
            $custom_url = 'https://example.com/installer';

            // Modify the link array with the custom URL
            $links[$index]['url'] = $custom_url;
			
		} elseif( $link['text'] === 'Competitors' ) {
			// Set the custom URL for breadcrumb
            $custom_url = 'https://example.com/competitor';

            // Modify the link array with the custom URL
            $links[$index]['url'] = $custom_url;
			
		}
    }

    return $links;
}

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