Skip to content

Instantly share code, notes, and snippets.

@lukasfarina
Last active September 18, 2018 12:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukasfarina/83cdb2e8c2661b2b8017 to your computer and use it in GitHub Desktop.
Save lukasfarina/83cdb2e8c2661b2b8017 to your computer and use it in GitHub Desktop.
PT-BR: Trocar o output do Breadcrumb do plugin Yoast para uma versão com microdata. | EN: Change output of your Yoast breadcrumbs to version with microdata
<?php
/*
* Plugin Name: Fix Microdata Yoast Breadcrumb
* Plugin URI: https://gist.github.com/lukasfarina/83cdb2e8c2661b2b8017/
* Author: Lucas Farina
* Author URI: lucasfarina.com.br
* */
// Change Wrap of Breadcrumb
function filter_wpseo_breadcrumb_output ($output) {
$html = '<ol itemscope="" itemtype="http://schema.org/BreadcrumbList">';
// New DOMObject
$dom = new DOMDocument('1.0', 'UTF-8');
/// Load HTML
$dom->loadHTML(mb_convert_encoding($output, 'HTML-ENTITIES', 'UTF-8'));
// Disable Errors
libxml_use_internal_errors(true);
$childCount = 0;
// Get list of <a> childs
if($dom->getElementsByTagName('a')->length) {
foreach($dom->getElementsByTagName('a') as $a){
$html .= "
<li itemprop=\"itemListElement\" itemscope itemtype=\"http://schema.org/ListItem\">
<a itemprop=\"item\" href='{$a->getAttribute('href')}'>
<span itemprop=\"name\">
{$a->nodeValue}
</span>
<meta itemprop=\"position\" content=\"{$childCount}\" />
</a>
</li>";
$childCount++;
}
}
// Instance finder.
$finder = new DomXPath($dom);
$classname = 'breadcrumb_last';
// Search by breadcrumb_last <span>
if($span = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]")->item(0)) {
$html .= "
<li itemprop=\"itemListElement\" itemscope itemtype=\"http://schema.org/ListItem\">
<strong itemprop=\"item\">
<span itemprop=\"name\">
{$span->nodeValue}
</span>
<meta itemprop=\"position\" content=\"{$childCount}\" />
</strong>
</li>";
}
// Clear Errors
libxml_clear_errors();
$html .= '</ol>';
return $html;
}
// add the filter 
add_filter('wpseo_breadcrumb_output', 'filter_wpseo_breadcrumb_output', 10, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment