Add CSS class to all images in Joomla articles using PHP -
Add CSS class to all images in Joomla articles using PHP -
i have installed plugin called nice watermark (on joomla 3 site) adds watermark images class (in case <img class="waterm" src="/images/wm/idx-homepage3cfotoby0.jpg" alt="">
)
possible utilize php add together css class images before site loaded? or better, target images part of article (i don't want watermark icons , other template images)?
i thinking plugin finds <img src=.../>
tags in code , adds class, i'm not sure how attack problem. maybe can point me in right direction?
edit:
i've created joomla plugin next code, , works, adds finish html construction (<!doctype html public "-//w3c//dtd html 4.0 transitional//en" "http://www.w3.org/tr/rec-html40/loose.dtd"> <html><body><p><img...
around images. how can avoid this?
<?php // no direct access defined('_jexec') or die; jimport('joomla.plugin.plugin'); class plgcontentaddimageclass extends jplugin { public function __construct(&$subject, $params) { parent::__construct($subject, $params); } public function oncontentprepare($context, &$article, &$params, $offset = 0) { $content =& $article->text; // article content $dom = new domdocument(); @$dom->loadhtml($content); $dom->preservewhitespace = false; $images = $dom->getelementsbytagname('img'); foreach($images $image) { // existing classes on images $existing_classes = $image->getattribute('class'); // class we're adding $new_class = ' watermark'; // existing classes plus new class $class_names_to_add = $existing_classes . $new_class; $image->setattribute('class', $class_names_to_add); } $content = $dom->savehtml(); homecoming true; } } ?>
according php manual, savehtml()
method automatically adds <html>
, <body>
, <!doctype>
tags output, found code snippet on same page removes these codes. here's finish code worked me:
<?php // no direct access defined('_jexec') or die; jimport('joomla.plugin.plugin'); class plgcontentaddimageclass extends jplugin { public function __construct(&$subject, $params) { parent::__construct($subject, $params); } public function oncontentprepare($context, &$article, &$params, $offset = 0) { // article content $content = &$article->text; $dom = new domdocument(); @$dom->loadhtml(mb_convert_encoding($content, 'html-entities', 'utf-8')); $dom->preservewhitespace = false; $images = $dom->getelementsbytagname('img'); foreach($images $image) { $existing_classes = $image->getattribute('class'); // class we're adding $new_class = ' watermark'; // existing classes plus new class $class_names_to_add = $existing_classes . $new_class; $image->setattribute('class', $class_names_to_add); } // remove unwanted tags $content = preg_replace('/^<!doctype.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->savehtml())); homecoming true; } } ?>
php joomla
Comments
Post a Comment