Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
}
@mstudioIL
Copy link

mstudioIL commented Jun 26, 2022

How can I make the last items as link?

@joseEkcit
Copy link

joseEkcit commented Jul 19, 2022

Returning false on wpseo_breadcrumb_links triggers PHP warnings.

image

Is there an updated way to do this?

Returning an array with empty (but set) 'url' and 'text' seems to solve the warning. Like: return array('url'=>'', 'text'=>'')

@amboutwe
Copy link
Author

amboutwe commented Jul 21, 2022

@joseEkcit Thank you for providing more information. I've updated the snippet now.

@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

yes-or-not commented Sep 19, 2022

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

vivekkotadiya commented Sep 20, 2022

@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

yes-or-not commented Sep 20, 2022

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

vivekkotadiya commented Sep 20, 2022

@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

yes-or-not commented Sep 20, 2022

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

vivekkotadiya commented Sep 20, 2022

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

@yes-or-not
Copy link

yes-or-not commented Sep 20, 2022

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

vivekkotadiya commented Sep 20, 2022

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

yes-or-not commented Sep 20, 2022

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

yes-or-not commented Sep 20, 2022

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

vivekkotadiya commented Sep 26, 2022

@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( '

%1$s

',
sprintf( __( 'The %1$s 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 echo '

';print_r($links);die; before unset($links[0]);? If possible put this and see what output returns.

@yes-or-not
Copy link

yes-or-not commented Sep 26, 2022

where should I put this commend?

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