Notice: This thread is over two years old; the information may be outdated. Please consider creating a new thread if you require free support. If you have an active SiteOrigin Premium license, you can email our premium support desk at [email protected].
I am using a lightbox plugin that requires class=“envirabox”. Unfortunately, the plugin doesn’t automatically add the class to existing image links like some of the other lightbox plugins do. So, I need to add the class to all images. I have a php function but it’s not working. In some cases, the images already have a class, so I need to add the envirabox class in. Maybe you guys can help me here.
function add_class($content) { global $post; $pattern = "/(]*?class=['"].*)[^>]*?href=['"][^'"]+?.(?:bmp|gif|jpg|jpeg|png)['"][^>]*)>/i"; $replacement = '$1 class="envirabox">'; $content = preg_replace($pattern, $replacement, $content); return $content; } add_filter("the_content","add_class");
Hi Chris
The developers of the lightbox plugin might be in a better position to help you with this. Perhaps their plugin has an option somewhere to change their Javascript selector to look for image links, instead of links with the class envirabox. This is a fairly trivial change in JS.
If you have to make this change in PHP, and I’d consider this a last resort because it creates a lot of overhead, using Dom Document would be a much better solution. Here’s a basic guide on using Dom Document to add classes.
http://stackoverflow.com/questions/7056624/how-to-add-class-to-an-img-object-in-dom-php
If you’re not all that comfortable in PHP, then it might be a good idea to hire a developer from somewhere like Codeable to help you with this.