This thread is over two years old and may be outdated. Please create a new thread if you need help, or email us if you have an active Premium license.

adding an image class to all images to make lightbox work

10 years ago · Last reply by Greg Priday 10 years ago

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.

Need fast email support? Get SiteOrigin Premium

Replies

1
  1. Greg Priday Staff 10 years, 2 months ago

    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.

Have a different question or issue?

Start New Thread