Skip to content

Instantly share code, notes, and snippets.

Last active March 31, 2023 11:57
Show Gist options
  • Save lbgm/b0a5ef1da3362a0b6238a91e1d76d020 to your computer and use it in GitHub Desktop.
Save lbgm/b0a5ef1da3362a0b6238a91e1d76d020 to your computer and use it in GitHub Desktop.
Generate breadcrumbs with PHP, Usable for Google structured data

Using with Google Tag Manager

Place this code before the integration (as high as possible) of Google Tag Manager on your pages:


You will now have two variables (SiteBreadcrumb and Breadcrumb) in the dataLayer of your container.

in GTM: With the SiteBreadcrumb variable, you will be able to identify eligible pages with a trigger; Then get the Breadcrumb variable and with this content in a custom tag, everything is done:

var ld =
 "@context": "",
"@type": "BreadcrumbList",
"itemListElement": {{Breadcrumb}}
var s= document.createElement('script');
    s.setAttribute('type', 'application/ld+json');
    s.textContent = JSON.stringify(ld);

Using Without Google Tag Manager

Put this snippet of code in the desired location and use Google's Rich Results Testing Tool to preview the result.

#Breadcrumb List Generator
class Breadcrumb
public static function c()
static $instance;
$instance= new Breadcrumb();
return $instance;
private function rglob($no,$root)
#find root firts elements
$objects = $this->sanitize($no,array_merge(glob("$root*.php",GLOB_NOSORT),glob("$root*",GLOB_ONLYDIR)));
#find each root first element sub
foreach ($objects as $object)
if(is_dir($object) && !is_link($object)) $objects= $this->sanitize($no,array_merge($objects,glob("$object/*",GLOB_NOSORT|GLOB_ONLYDIR)));
return $objects;
private function getItems()
#Exclusions. to exclude unwanted file or dir. add more yourself
#Translations. add more yourself
#for breadcrumb items store
#operating root, script path
#only dir aloowed
#get all dirs, subdirs and files
$objects = $this->rglob($no,$root);
foreach($objects as $i=>$object){
#when $object has valid name
if($object != "." && $object != "..")
#create an array for item real positions from PATH
#translate name
#breadcrumb item
$_barr[]="\r\n{\"@type\": \"ListItem\",\r\n\"position\": $index,\r\n\"name\": \"$n\",\r\n\"item\": \"$q\"\r\n}\r\n";
#implode items with comma and return
$_barr=implode(",", $_barr);
return $_barr;
public function dataLayer()
#for Google Tag Manager dataLayer
return "\r\n<script>\r\n(dataLayer= window.dataLayer || []).push({\r\n'SiteBreadcrumb':'allowed',\r\n'Breadcrumb': [$g]\r\n});\r\n</script>\r\n";
public function List()
#for Google ld+json directly
return "\r\n<script type=\"application/ld+json\">\r\n{\r\n\"@context\": \"\"\r\n,\"@type\": \"BreadcrumbList\"\r\n,\"itemListElement\": [$g]}\r\n</script>\r\n";
private function sanitize($no,$aa)
#remove unwanted items from glob results
foreach ($no as $a => $x) {
foreach ($aa as $b => $y) {
# code...
if(strpos($y, $x)!==false)
return array_values($f);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment