Gallery & Image Block Lightbox

Описание

This WordPress Plugin adds a simple & lightweight Lightbox to the standard WordPress Gallery & Image Block. It detects if a post contains a Gallery or Image Block and then loads & initialize baguetteBox.js, a pure JavaScript Lightbox script without any dependencies and a modern minimal look.

There is no lock in effect. It does not add a new Block Editor/Gutenberg Block and relies solo on the standard WordPress Gallery and Image Block. You can deactivate this plugin at any time and your Gallery and Image Blocks will work as they’ve done before. Just without the lightbox.

Just one important thing to remember: You always have to select Link toMedia File for all of your Galleries & Images to work properly. If you choose None or Attachment Page it will not work. But that’s fine if you don’t want the Lightbox for a specific Image or Gallery.

Features

Here are the features of baguetteBox.js:

  • Written in pure JavaScript
  • No dependencies (e.g. jQuery)
  • Image captions support
  • Responsive
  • Supports swipe gestures
  • Around 13 KB, 7 KB gzipped

Works with

  • Gallery Block (Default Block)
  • Image Block (Default Block)
  • Media and Text Block (Default Block)
  • Gallery (Default Classic Editor Gallery)
  • CoBlocks Gallery Collage / Masonry / Offset / Stacked

Don’t forget to select Link to → Media File for all of your Galleries & Images to work properly.

Notice: At the moment, just Blocks inside a post are supported. Not Blocks in a Widget. You can make it work but have to enqueue the necessary style & script yourself. See FAQ —> How to enqueue the necessary script & style for blocks outside of posts?

Скриншоты

  • Simple, modern and minimalistic Lightbox for your Gallery Block
  • Set Link to to Media File on your Gallery Block

Установка

  1. Upload the folder gallery-block-lightbox to the /wp-content/plugins/ directory
  2. Activate the plugin through the ‘Plugins’ menu in WordPress
  3. Add a Gallery or Image Block to your Post and set Link toMedia File
  4. Check your Gallery or Image with Lightbox on the front end by clicking on an image

Часто задаваемые вопросы

When I click on an image, nothing happens

Check if you used the built in Gallery Block and if Link to is set to Media File in the Block sidebar. On an Image Block choose Add Link at the Toolbar.

Does it work for single images?

Yes! Just set the Link at the Toolbar to Media File

Can I deactivate the Lightbox for a single Image or Gallery?

Set Media File to None or remove the Link.

Is there a way to set the default Link to option to Media File?

Since WordPress 5.6 you can now set the default behavior for Link to. Go to yourblog.com/wp-admin/options.php and search for image_default_link_type. Set the value to file and hit save. This will apply to all new Image & Gallery Blocks.

Or you can add the follow snipped (WordPress 5.7+ / PHP 7.4+) to your functions.php:

add_filter('option_image_default_link_type', fn () => 'file');

How can I add my own Block? / Can I change the CSS selector?

You can change the CSS selector to a gallery (or galleries) containing <a> tags used by baguetteBox.js with the baguettebox_selector filter:

apply_filter( 'baguettebox_selector', function($selector) { return $selector . ',.my-gallery'; } )

You can override the full selector by just returing your selector e.g. to show all images in your post in one lightbox (not per Gallery/Image Block):

apply_filter( 'baguettebox_selector', function() { return '.entry-content'; } )

How to enqueue the necessary script & style for blocks outside of posts?

If you use a Gallery or Image Block outside a post e.g. inside a Widget and want to apply the Lightbox, you have to make sure to enqueue the necessary script & style.

If the Widget is on every page or the majority of sites, you can just enqueue the script baguettebox & style baguettebox-css everywhere. Just add the following action to your functions.php:

add_action( 'wp_enqueue_scripts', function () { wp_enqueue_script( 'baguettebox' ); wp_enqueue_style( 'baguettebox-css' ); } );

If your Widget is just at the front page, you can add a check for is_front_page():

add_action( 'wp_enqueue_scripts', function () { if ( is_front_page() ) { wp_enqueue_script( 'baguettebox' ); wp_enqueue_style( 'baguettebox-css' ); } } );

Отзывы

09.01.2022
You are the man! This makes the WP Gallery what it should be. WP should have built this in. They want you to buy jetpack though.
29.12.2021
This plugin adds a lightbox to your WP galleries. Simple and lightweight. It works out of the box and does exactly what it says. However, keep in mind that you have no settings whatsoever. You can, of course, tweak things via CSS. For me, this is the perfect approach, since you don't have to bloat the plugin just for some settings users can change in the WP dashboard. But that may not be everybody's cup of tea.
15.12.2021
Thank you so much! In the past I was using "Simple Lightbox", but that plugin doesn't work anymore with the new image blocks I guess. Anyway, your plugin works great! I finally got my nice lightbox back again. Thank you. Regards, Melroy
23.11.2021
I just wanted to add a lightbox to the standard WordPress Gallery block and was getting frustrated with the first couple of plugins I tried. This one worked right away and with minimum fuss. (Just ensure you set the image to link to the media file.)
22.11.2021
Took a while, until I discovered this plugin. That name is not the easiest to remember. But now, I got it: This is truely the simplest and lightest lightbox plugin, one can find. If it would include featured images and set links to media files by default, it would be perfect perfect, from my point of view. Anyway. Check it out!
Посмотреть все 43 отзыва

Участники и разработчики

«Gallery & Image Block Lightbox» — проект с открытым исходным кодом. В развитие плагина внесли свой вклад следующие участники:

Участники

«Gallery & Image Block Lightbox» переведён на 5 языков. Благодарим переводчиков за их работу.

Перевести «Gallery & Image Block Lightbox» на ваш язык.

Заинтересованы в разработке?

Посмотрите код, проверьте SVN репозиторий, или подпишитесь на журнал разработки по RSS.

Журнал изменений

1.10.1

  • Fix Image in Media & Text Block

1.10.0

  • Add two Filter baguettebox_selector for the baguetteBox.js CSS selector and baguettebox_filter for the baguetteBox.js file type RegExp filter

1.9.0

  • Support new Gallery Block format (will be released with WordPress 5.9 in December 2021) https://make.wordpress.org/core/2021/08/20/gallery-block-refactor-dev-note/

1.8.5

  • Fix Caption for the single Image Block

1.8.4

  • Add Caption Support for the Classic Editor Gallery

1.8.3

  • Fix Regex Filter

1.8.2

  • Add support for file links with query parameter at the end

1.8.1

  • Fix image link filter

1.8.0

  • Add support for .avif, .heif/.heic, .tif

1.7.0

  • Add support for Media and Text Block

1.6.0

  • Add support for CoBlocks Gallery Collage / Masonry / Offset / Stacked

1.5.0

  • Add support for Classic Editor Gallery

1.4.0

  • Add support for .svg images

1.3.1

  • Remove trailing slash from register_script/style

1.3.0

  • Support for the default Image Block

1.2.0

  • Support HTML for the Caption

1.0.0

First release