Skip to content

Instantly share code, notes, and snippets.

@jonathanjanssens
Created December 17, 2015 12:27
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jonathanjanssens/225181fad721d6d136af to your computer and use it in GitHub Desktop.
Save jonathanjanssens/225181fad721d6d136af to your computer and use it in GitHub Desktop.
Add schema to yoast seo breadcrumbs
<?php
/**
* Add schema to yoast seo breadcrumb
*/
add_filter('wpseo_breadcrumb_links', 'jj_add_crumb_schema', 10, 1);
function jj_add_crumb_schema($crumbs) {
if ( ! is_array( $crumbs ) || $crumbs === array() ) {
return $crumbs;
}
$listItems = [];
$j = 1;
foreach ( $crumbs as $i => $crumb ) {
$item = [];
if ( isset( $crumb['id'] ) ) {
$item = [
'@id' => get_permalink($crumb['id']),
'name' => strip_tags( get_the_title( $id ) )
];
}
if ( isset( $crumb['term'] ) ) {
$term = $crumb['term'];
$item = [
'@id' => get_term_link( $term ),
'name' => $term->name
];
}
if ( isset( $crumb['ptarchive'] ) ) {
$postType = get_post_type_object($crumb['ptarchive']);
$item = [
'@id' => get_post_type_archive_link($crumb['ptarchive']),
'name' => $postType->label
];
}
if(isset($crumb['url'])) {
if($crumb['text'] !== '') {
$title = $crumb['text'];
} else {
$title = get_bloginfo('name');
}
$item = [
'@id' => $crumb['url'],
'name' => $title
];
}
$listItem = [
'@type' => 'ListItem',
'position' => $j,
'item' => $item
];
$listItems[] = $listItem;
$j++;
}
$schema = [
'@context' => 'http://schema.org',
'@type' => 'BreadcrumbList',
'itemListElement' => $listItems
];
$html = '<script type="application/ld+json">' . json_encode($schema) . '</script> ';
echo $html;
return $crumbs;
}
@jdmgarcia
Copy link

Thanks for this code. It's working perfectly.

@techsorsdev1
Copy link

Very cool solution, thanks a lot. The only question, is it real to make "name - Shop", except "name - Products"?
03728ec505
Thank you very much.

@techsorsdev1
Copy link

Aha, I did it, thanks for inspiration:)

@gaelgerard
Copy link

Hello, thanks for the code, it works like a charm on front end.

However, it creates an error in the dashboard which prevents assigning a post thumbnail.

Here is the error I get in console log when the code is active on my website:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Then when I try to assign a post thumbnail, it triggers the following error:

uncaught exception: Object

Can you please help

@zodit
Copy link

zodit commented Jul 4, 2020

Thank you, this is exactly what i want, just the BreadcrumbList. I use it together with the filter to disable the default Yoast WebPage Json-LD.

add_filter( 'wpseo_json_ld_output', '__return_false' );

My theme has the WebPage markup already in the page, and it was missing the BreadcrumbList.

@zodit
Copy link

zodit commented Jul 4, 2020

Hello, thanks for the code, it works like a charm on front end.

However, it creates an error in the dashboard which prevents assigning a post thumbnail.

Here is the error I get in console log when the code is active on my website:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Then when I try to assign a post thumbnail, it triggers the following error:

uncaught exception: Object

Can you please help

I am not sure what is your problem, and if it is related with the bug I discovered. There is an error in the code at line 25, it is not this:

'name' => strip_tags( get_the_title( $id ) )

but this:

'name' => strip_tags( get_the_title($crumb['id']) )

Of course, the variable $id is not defined and could give an error in front office.

@medzemson94
Copy link

also change echo $html; to print_r($html);

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