-
-
Save tommcfarlin/44178197b6878eb43e369cf6e5de09fc to your computer and use it in GitHub Desktop.
[WordPress] Modify Image Containers on the Server-Side with WordPress
This file contains 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 | |
namespace Acme; | |
use DOMDocument; | |
class AcmeContentSubscriber | |
{ | |
// ... | |
} |
This file contains 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 | |
public function contentSubscriber() | |
{ | |
add_action( 'the_content', [$this, 'addImageAttributes']); | |
} |
This file contains 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 | |
public 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)); | |
// ... | |
} |
This file contains 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 | |
public 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('class', 'acme-iamge'); | |
if ($image->nextSibling->tagName !== 'p') { | |
$this->wrapImage($document, $image); | |
} | |
} | |
// ... | |
} |
This file contains 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 | |
public 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('class', 'acme-image'); | |
if ($image->nextSibling->tagName !== 'p') { | |
$this->wrapImage($document, $image); | |
} | |
} | |
return $document->saveHTML(); | |
} |
This file contains 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 | |
/** | |
* This function is used to wrap individual images in a paragraph element. | |
* | |
* @param $document The DOM Document which is to be rendered. | |
* @param $image The image to wrap with the new paragraph element. | |
*/ | |
private function wrapImage($document, $image) | |
{ | |
if (null === $image) { | |
return; | |
} | |
$wrapper = $document->createElement('p'); | |
$wrapper->setAttribute('class', 'acme-image'); | |
$image->parentNode->replaceChild($wrapper, $image); | |
if (null !== $image) { | |
$wrapper->appendChild($image); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment