Skip to content

Instantly share code, notes, and snippets.

@sabrysuleiman
Last active May 28, 2019 09:10
Show Gist options
  • Save sabrysuleiman/25805f96eb82f2bad70aa1d07bdcb1da to your computer and use it in GitHub Desktop.
Save sabrysuleiman/25805f96eb82f2bad70aa1d07bdcb1da to your computer and use it in GitHub Desktop.
function to add Site Navigation schema to wordpress site footer. >> https://schema.org/SiteNavigationElement
<?php
###########################################################
# Sabry Navbar Schema function
###########################################################
if (!function_exists('sabryschemanav')) {
function sabryschemanav(){
$menuLocations = get_nav_menu_locations();
$menuID = $menuLocations['header-menu'];
$primaryNav = wp_get_nav_menu_items($menuID);
echo '<script type="application/ld+json">
{
"@context": "https://schema.org",
"@graph":
[';
$key4nav = 1;
foreach ( $primaryNav as $navItem ) {
if($key4nav != 1){echo',';}
echo '{
"@context": "https://schema.org",
"@type":"SiteNavigationElement",
"@id":"site-navigation-'.$key4nav.'",
"name": "'.$navItem->title.'",
"url": "'.replaculrslashes($navItem->url).'"
}';
$key4nav++;
}
echo '] } </script>';
}
}
function addsabryschemanav() {
sabryschemanav();
}
add_action( 'wp_footer', 'addsabryschemanav' );
function replaculrslashes($sjdkln) {
$kkkurl = str_replace("/","\/",$sjdkln);
return $kkkurl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment