Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin tommcfarlin/00-the-content.php Secret
Last active Oct 3, 2017

Embed
What would you like to do?
[WordPress] Adding Custom WordPress Image Attributes
<?php
private function addImageAttributes($content)
{
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$document = new \DOMDocument();
libxml_use_internal_errors(true);
$document->loadHTML(utf8_decode($content));
$images = $document->getElementsByTagName('img');
foreach ($images as $image) {
$image->setAttribute('data-example', 'true');
}
return $document->saveHTML();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.