Last active
September 18, 2018 12:10
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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