simplelightbox icon indicating copy to clipboard operation
simplelightbox copied to clipboard

Using Simple Light Box with WordPress Gutenberg?

Open paaljoachim opened this issue 4 years ago • 1 comments

Hi

How would I go about using Simple Light Box with Gutenberg? I am writing a tutorial on using lightboxes without installing a plugin and thought that this would be a fitting project to highlight. Thanks!

paaljoachim avatar Jul 29 '21 10:07 paaljoachim

FYI: There is the ready-made Wordpress plugin Responsive Gallery Grid which incorporates SimpleLightBox! Maybe it suits you.

porg avatar Aug 21 '21 01:08 porg

I think it just works. You can use the simplelightbox wordpress plugin or write it on your own with something like this:

in your functions.php

add_filter('the_content', [$this, 'autoexpand_rel_wlightbox'], 99);
add_filter('the_excerpt', [$this, 'autoexpand_rel_wlightbox'], 99);

public function autoexpand_rel_wlightbox ($content) {
    global $post;
    $pattern     = "/(<a(?![^>]*?rel=['\"]lightbox.*)[^>]*?href=['\"][^'\"]+?\.(?:bmp|gif|jpg|jpeg|png|webp)['\"][^\>]*)>/i";
    $replacement = '$1 class="simplelightbox" rel="lightbox['.$post->ID.']">';
    $content     = preg_replace($pattern, $replacement, $content);
    return $content;
}

and in your javascript

if(document.querySelectorAll('a.simplelightbox').length ) {
    let simplelightbox = new SimpleLightbox('a.simplelightbox',{});
}

andreknieriem avatar Oct 21 '22 07:10 andreknieriem

Hi @andreknieriem Andre

I began writing a tutorial to add to this article: https://easywebdesigntutorials.com/adding-a-lightbox-to-wordpress-without-using-a-plugin/

But the functions code produced an error.

/* Simple Light Box */
add_filter('the_content', [$this, 'autoexpand_rel_wlightbox'], 99);
add_filter('the_excerpt', [$this, 'autoexpand_rel_wlightbox'], 99);

public function autoexpand_rel_wlightbox ($content) {
    global $post;
    $pattern     = "/(<a(?![^>]*?rel=['\"]lightbox.*)[^>]*?href=['\"][^'\"]+?\.(?:bmp|gif|jpg|jpeg|png|webp)['\"][^\>]*)>/i";
    $replacement = '$1 class="simplelightbox" rel="lightbox['.$post->ID.']">';
    $content     = preg_replace($pattern, $replacement, $content);
    return $content;
}

Is giving this error:

Your PHP code changes were rolled back due to an error on line 15 of file wp-content/themes/blocksy-child/functions.php. Please fix and try saving again.

syntax error, unexpected 'public' (T_PUBLIC), expecting end of file

paaljoachim avatar Nov 01 '22 13:11 paaljoachim

Hi @andreknieriem Andre

I began writing a tutorial to add to this article: https://easywebdesigntutorials.com/adding-a-lightbox-to-wordpress-without-using-a-plugin/

But the functions code produced an error.

/* Simple Light Box */
add_filter('the_content', [$this, 'autoexpand_rel_wlightbox'], 99);
add_filter('the_excerpt', [$this, 'autoexpand_rel_wlightbox'], 99);

public function autoexpand_rel_wlightbox ($content) {
    global $post;
    $pattern     = "/(<a(?![^>]*?rel=['\"]lightbox.*)[^>]*?href=['\"][^'\"]+?\.(?:bmp|gif|jpg|jpeg|png|webp)['\"][^\>]*)>/i";
    $replacement = '$1 class="simplelightbox" rel="lightbox['.$post->ID.']">';
    $content     = preg_replace($pattern, $replacement, $content);
    return $content;
}

Is giving this error:

Your PHP code changes were rolled back due to an error on line 15 of file wp-content/themes/blocksy-child/functions.php. Please fix and try saving again.

syntax error, unexpected 'public' (T_PUBLIC), expecting end of file

I think you need to remove the public access modifier, since modifiers are needed for methods inside the class.

In your case, it's just a function

olegopro avatar Nov 03 '22 15:11 olegopro

Hi @olegopro Oleg

Can you add the adjusted code example so that I can try it out?

paaljoachim avatar Nov 03 '22 15:11 paaljoachim