Yoast Breadcrumbs on WordPress sites not validating

If you have been developing a WordPress site recently you may have found that when you activate the breadcrumbs function in the Yoast SEO Plugin, the site no longer validates with W3C. This has been a previous issue on Yoast a couple of years ago, but the problem seems to have arisen again recently.

With the help of a simple function (written by WP user jaikdean) you no longer have to worry about it not validating.

Just copy the function below and paste it into the functions.php file in your WordPress theme.

 * Convert Yoast breadcrumbs to use Microdata
 * @params string $breadcrumbs Breadcrumb HTML
 * @return string
 * @author Jaik Dean
function convertBreadcrumbsToMicrodata($breadcrumbs)
    // remove the XML namespace
    $breadcrumbs = str_replace(' xmlns:v=""', '', $breadcrumbs);

    // convert each breadcrumb
    $breadcrumbs = preg_replace(
        '/<span typeof="v:Breadcrumb"><a href="([^"]+)" rel="v:url" property="v:title">([^<]+)<\\/a><\\/span>/',
        '<span itemscope itemtype=""><a href="$1" itemprop="url"><span itemprop="title">$2</span></a></span>',

    $breadcrumbs = preg_replace(
        '/<span typeof="v:Breadcrumb"><span class="breadcrumb_last" property="v:title">([^<]+)<\\/span><\\/span>/',
        '<span itemscope itemtype=""><span class="breadcrumb_last" itemprop="title">$1</span></span>',

    return $breadcrumbs;

add_filter('wpseo_breadcrumb_output', 'convertBreadcrumbsToMicrodata');

(You can find the topic on the WordPress codex here.)

Once you have uploaded the file you should find that you no longer get those validation errors!

It really is that simple.

Let us know if it works for you by writing in the comments below.