Enable WordPress Native Image Lightbox And Apply To All Existing Images

Lightbox, now a built-in WordPress feature, enlarges images on click. Enable it easily by inserting an image and clicking the link button. For existing images, use SQL to add the lightbox attribute. Back up wp_posts table first, then run a query to find images without lightbox and update them accordingly.

Lightbox is a feature that can enlarge an image when clicking on it. After WordPress 6.X, lightbox became a built-in feature. We don’t have to install any additional plugins to enable it. The way to use the feature is simple. Just insert an image and click the link button.

Is It Possible To Enable Lightbox To All Existing Images With One Click?

Yes. We can do it with SQL. Before starting it, I STRONGLY RECOMMEND you back up the wp_posts table.

First, when you enter the MySQL and check wp_posts, you can view how images are embedded in the post_content column.

<!-- wp:image {"lightbox":{"enabled":true},"id":6828,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://aronhack.com/wp-content/uploads/add-service-form-1920x1045.png" alt="" class="wp-image-6828"/></figure>
<!-- /wp:image -->
<!-- wp:image {"id":6823,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://aronhack.com/wp-content/uploads/image-213-1920x1045.png" alt="" class="wp-image-6823"/></figure>
<!-- /wp:image -->

Images have an attribute lightbox":{"enabled":true} once the lightbox is enabled. We can search for images without the attribute using the following SQL query.

SELECT * FROM wp_posts
WHERE post_content LIKE '%<!-- wp:image {"id"%'
AND post_type = 'post'
ORDER BY ID DESC;

If the query result seems correct, we can go further to enable the lightbox for all existing images.

UPDATE wp_posts
SET post_content = REPLACE(post_content, '<!-- wp:image {"id"', '<!-- wp:image {"lightbox":{"enabled":true},"id"')
WHERE post_content LIKE '%<!-- wp:image {"id"%'
AND post_type = 'post'

🚀 Unlock Ads-Free Experience At $5/year

✓ 14 days free trial ✓ Cancel anytime

1 thought on “Enable WordPress Native Image Lightbox And Apply To All Existing Images”

  1. This is a brilliant and efficient workaround for updating legacy content without cluttering the site with additional light, single-purpose plugins. Emphasizing the backup of the wp_posts table prior to executing the UPDATE query is a crucial safety callout—running raw string replacements on core database tables can easily corrupt block markup if a single string match goes wrong!

Leave a Comment

Your email address will not be published. Required fields are marked *