Last active
February 7, 2019 09:15
-
-
Save keslo/00074660fce2b57687c7410fbdde58fe to your computer and use it in GitHub Desktop.
Snippet microdata scheme.org for images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
if($modx->event->name=='OnWebPagePrerender'){ | |
libxml_use_internal_errors(true); | |
$url = $_SERVER['REQUEST_URI']; | |
if (strpos($url, '.xml') === false) { | |
// атрибуты изображения для копирования | |
$attributes = array('src','class','style', 'alt'); | |
// получаем HTML-код страницы | |
$content = $modx->documentOutput; | |
// формируем DOM | |
$dom = new DOMDocument(); | |
$dom->loadHTML($content); | |
$main = $dom->getElementsByTagName('main'); | |
// получаем массив изображений на странице | |
$img = $main[0]->getElementsByTagName('img'); | |
foreach ($img as $item) { | |
$node = $dom->createElement('div'); | |
$node->setAttribute('itemscope', 'true'); | |
$node->setAttribute('itemtype' ,'http://schema.org/ImageObject'); | |
// meta itemprop="name" | |
$title = $dom->createElement('meta'); | |
$title->setAttribute('itemprop', 'name'); | |
$title->setAttribute('content', $item->getAttribute('alt')); | |
$node->appendChild($title); | |
// img itemprop="url" | |
$newImg = $dom->createElement('img'); | |
foreach ($attributes as $attr) { | |
$newImg->setAttribute($attr, $item->getAttribute($attr)); | |
} | |
$newImg->setAttribute('itemprop', 'url'); | |
$node->appendChild($newImg); | |
// span itemprop="contentUrl" | |
$span = $dom->createElement('span'); | |
$span->setAttribute('style', 'display:none'); | |
$span->setAttribute('itemprop', 'contentUrl'); | |
$span->setAttribute('href', $_SERVER['HTTP_HOST'].'/'.$item->getAttribute('src')); | |
$node->appendChild($span); | |
// meta itemprop="description" | |
$desc = $dom->createElement('meta'); | |
$desc->setAttribute('itemprop', 'description'); | |
$desc->setAttribute('content', $item->getAttribute('alt')); | |
$node->appendChild($desc); | |
$item->parentNode->replaceChild($node, $item); | |
} | |
// выводим содержимое HTML | |
$modx->documentOutput = $dom->saveHTML(); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment