Fun with bcn_display_json_ld() — the Basics

Introduced in Breadcrumb NavXT 5.7.0, bcn_display_json_ld() is the newest member of the bcn_display* family. Since it outputs the breadcrumb trail in a Schema.org BreadcrumbList compliant JSON-LD format, there are numerous uses for this function.

Basic

To simply output the JSON-LD breadcrumb trail, visible to search engines but hidden from users, the following can be placed in the currently active theme’s header.php in the block.

<?php if (function_exists(‘bcn_display_json_ld()’)) {?>
<script type="application/ld+json"><?php bcn_display_json_ld();?></script>
<?php } ?>

Enhanced Basic

Rather than modifying a theme file, a hook into wp_head can be used. The benefit of this approach is it will work across theme upgrades and migrations. This requires placing the following code in a site specific plugin:

add_action('wp_head', 'my_json_ld_breadcrumb_trail', 10);
function my_json_ld_breadcrumb_trail()
{
	if (function_exists(‘bcn_display_json_ld()’))
	{
		printf('<script type="application/ld+json">%s</script>', bcn_display_json_ld(true));
	}
}

Reaching Farther

The two examples that were just covered output the JSON-LD directly into the HTML document. This need not be the case. One could write a REST API endpoint and use it to return the JSON-LD data. However, doing this is a daunting task when working with Breadcrumb NavXT 5.x. This situation improves in Breadcrumb NavXT 6.0. Still, in 6.0, it requires directly accessing many methods within the bcn_breadcrumb_trail class. That said, it should be possible to write an extension targeting Breadcrumb NavXT 6.0 that does this. In a future article, this idea will be explored in greater depth.

-John Havlik

[end of transmission, stay tuned]

Posted in Guides
Updated: