Home>Support>adding an image class to all images to make lightbox work

adding an image class to all images to make lightbox work

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");

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 9 years, 7 months ago Greg Priday
    Hi, I Work Here

    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.

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More