-
-
Save tommcfarlin/52ba9093940abf03519d77cd17dfa257 to your computer and use it in GitHub Desktop.
[WordPress] Adding Custom WordPress Image Attributes
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
| <?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(); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This isn't really designed to be used in a child theme - instead, it's meant for an object-oriented plugin so you'd have to refactor it a bit if you wanted it to work in the context of a theme.
Namely, you'd need to:
privatefunction the functionDOMDocumentis available in your installation of PHPechothe code that's returned from this functionIt's a lot looking at this way but hopefully it gives you a starting point!