Работа с отложенными функциями - amel-post/bitrix.help GitHub Wiki

Вывод og тегов с использованием отложенных функций

//init.php
function showOgMeta()
{
    global $APPLICATION;

    $arOg = [];

    $arOg['og:title'] = $APPLICATION->GetPageProperty('og:title') !== false ?
        $APPLICATION->GetPageProperty('og:title') : $APPLICATION->GetProperty('title');

    $arOg['og:description'] = $APPLICATION->GetPageProperty('og:description') !== false ?
        $APPLICATION->GetPageProperty('og:description') : $APPLICATION->GetProperty('description');

    if(empty($arOg['og:description']) && !empty($arOg['og:title'])){
        $arOg['og:description'] = $arOg['og:title'];
    }

    if($APPLICATION->GetPageProperty('og:image') !== false){
        $arOg['og:image'] = $APPLICATION->GetPageProperty('og:image');
    }

    if($APPLICATION->GetPageProperty('og:image:width') !== false){
        $arOg['og:image:width'] = $APPLICATION->GetPageProperty('og:image:width');
    }

    if($APPLICATION->GetPageProperty('og:image:height') !== false){
        $arOg['og:image:height'] = $APPLICATION->GetPageProperty('og:image:height');
    }

    if($APPLICATION->GetPageProperty('og:type') !== false){
        $arOg['og:type'] = $APPLICATION->GetPageProperty('og:type');
    }

    if($APPLICATION->GetPageProperty('og:url') !== false){
        $arOg['og:url'] = $APPLICATION->GetPageProperty('og:url');
    }

    $ogContent = '';
    foreach($arOg as $ogName => $ogValue){
        $ogContent .= '    <meta property="'.$ogName.'" content="' . htmlspecialchars(strip_tags($ogValue)) . '"/>' . PHP_EOL;
    }

    return $ogContent;
}

header.php

$APPLICATION->AddBufferContent('showOgMeta');

index.php

$APPLICATION->SetPageProperty('og:type', 'website');