Skip to content

Instantly share code, notes, and snippets.

@EscApp2
Created June 6, 2025 14:54
Show Gist options
  • Select an option

  • Save EscApp2/e609ac2e52831ad4d846d0a2cd97042a to your computer and use it in GitHub Desktop.

Select an option

Save EscApp2/e609ac2e52831ad4d846d0a2cd97042a to your computer and use it in GitHub Desktop.
breadcrumbs schema.org
<?
AddEventHandler("main", "OnEndBufferContent", "AddMicrodataBreadCrumbs",99999);
function AddMicrodataBreadCrumbs(&$content){
global $APPLICATION;
if(defined('ADMIN_SECTION')){
return true;
}
$dir = $APPLICATION->GetCurDir(false);
if(strpos($dir, '/bitrix/admin/') !== false){
return true;
}
if(strpos($dir, '/bitrix/gadgets/') !== false){
return true;
}
if(strpos($dir, '/bitrix/tools/') !== false){
return true;
}
if(strpos($dir, '/upload/') !== false){
return true;
}
if(
!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
){
return true;
}
if($_SERVER['REQUEST_METHOD'] != "GET"){
return true;
}
$path = $APPLICATION->GetCurPage();
$template = SITE_TEMPLATE_PATH."/schema_breadcrumbs.php";
$schema = $APPLICATION->GetNavChain($path , 0 , $template, true, false);
$content = AddScriptCustom::addHeadScript($schema, $content);
}
class AddScriptCustom
{
//from arturgolubev.abandoned unitools class
static function addHeadScript($script, $oldBuffer)
{
$search = '</head>';
$replace = $script . PHP_EOL . $search;
$bufferContent = $oldBuffer;
if (substr_count($oldBuffer, $search) == 1) {
$bufferContent = str_replace($search, $replace, $oldBuffer);
} else {
$bodyEnd = self::getLastPositionIgnoreCase($oldBuffer, $search);
if ($bodyEnd !== false) {
$bufferContent = substr_replace($oldBuffer, $replace, $bodyEnd, strlen($search));
}
}
return $bufferContent;
}
static function addBodyScript($script, $oldBuffer)
{
$search = '</body>';
$replace = $script . PHP_EOL . $search;
$bufferContent = $oldBuffer;
if (substr_count($oldBuffer, $search) == 1) {
$bufferContent = str_replace($search, $replace, $oldBuffer);
} else {
$bodyEnd = self::getLastPositionIgnoreCase($oldBuffer, $search);
if ($bodyEnd !== false) {
$bufferContent = substr_replace($oldBuffer, $replace, $bodyEnd, strlen($search));
}
}
return $bufferContent;
}
static function getLastPositionIgnoreCase($haystack, $needle, $offset = 0)
{
if (defined("BX_UTF")) {
if (function_exists("mb_orig_strripos")) {
return mb_orig_strripos($haystack, $needle, $offset);
}
return mb_strripos($haystack, $needle, $offset, "latin1");
}
return strripos($haystack, $needle, $offset);
}
static function getFirstPositionIgnoreCase($haystack, $needle, $offset = 0)
{
if (defined("BX_UTF")) {
if (function_exists("mb_orig_stripos")) {
return mb_orig_stripos($haystack, $needle, $offset);
}
return mb_stripos($haystack, $needle, $offset, "latin1");
}
return stripos($haystack, $needle, $offset);
}
}
<?php
if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
/**
* @global CMain $APPLICATION
*/
global $APPLICATION;
//delayed function must return a string
if(empty($arResult))
return "";
$strReturn = '<script type="application/ld+json">';
$arData = array(
"@context"=>"https://schema.org",
"@type"=>"BreadcrumbList",
"itemListElement"=>[],
);
$itemSize = count($arResult);
for($index = 0; $index < $itemSize; $index++)
{
$title = htmlspecialcharsex($arResult[$index]["TITLE"]);
$href = $arResult[$index]["LINK"];
if(empty($href) && $index == $itemSize-1){
$href = $APPLICATION->GetCurPage();
}
if(!empty($href)){
$href = URN2URI($href);
}
$arData['itemListElement'][] = [
"@type"=> "ListItem",
"position"=> $index,
"item"=>[
"name"=>$title,
"@id"=>$href,
],
];
}
$strReturn .= json_encode($arData);
$strReturn .= '</script>';
return $strReturn;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment