Lnsel Post Gallery Lightbox
/* Plugin Name: Lnsel Post Gallery Lightbox Plugin URI: Description: …
Install in
/** * Plugin Name: Lnsel Post Gallery Lightbox * Plugin URI: https://wordpress.org/plugins/lnsel-post-gallery-lightbox/ * Description: Adds a custom Gallery meta box in the post editor. Upload images per-post and display them as a responsive Grid or Masonry lightbox gallery using the [lnsel_gallery] shortcode. The gallery section is automatically hidden when no images are added. * Version: 1.0.6 * Requires at least: 5.8 * Requires PHP: 7.4 * Author: team-lnsel * Author URI: https://profiles.wordpress.org/team-lnsel/ * License: GPL-2.0-or-later * License URI: https://www.gnu.org/licenses/gpl-2.0.html * Text Domain: lnsel-post-gallery-lightbox * Tested up to: 7.2 / if ( ! defined( ‘ABSPATH’ ) ) exit; define( ‘LNSEL_PGL_VERSION’, ‘1.0.6’ ); define( ‘LNSEL_PGL_PLUGIN_DIR’, plugin_dir_path( FILE ) ); define( ‘LNSEL_PGL_PLUGIN_URL’, plugin_dir_url( FILE ) ); / ═══════════════════════════════════════════ 1. META BOX ═══════════════════════════════════════════ */ /** * Post types that get the gallery meta box. * Defaults to post, page and every public custom post type. * Filter: add_filter( ‘lnsel_pgl_post_types’, fn( $types ) => array( ‘post’, ‘product’ ) ); */ function lnsel_pgl_get_post_types() { $types = get_post_types( array( ‘public’ => true ), ‘names’ ); unset( $types[‘attachment’] ); $types = apply_filters( ‘lnsel_pgl_post_types’, array_values( $types ) ); return is_array( $types ) ? array_map( ‘sanitize_key’, $types ) : array( ‘post’, ‘page’ ); } add_action( ‘add_meta_boxes’, ‘lnsel_pgl_register_meta_box’ ); function lnsel_pgl_register_meta_box() { add_meta_box( ‘lnsel_pgl_gallery_meta_box’, ‘📷 Post Gallery Images’, ‘lnsel_pgl_render_meta_box’, lnsel_pgl_get_post_types(), ‘normal’, ‘default’ ); } function lnsel_pgl_render_meta_box( $post ) { wp_nonce_field( ‘lnsel_pgl_save_gallery’, ‘lnsel_pgl_nonce’ ); $image_ids = get_post_meta( $post->ID, ‘_lnsel_pgl_gallery_ids’, true ); $ids_array = ! empty( $image_ids ) ? array_filter( explode( ‘,’, $image_ids ) ) : array(); $show_title = get_post_meta( $post->ID, ‘_lnsel_pgl_show_title’, true ); // Default to ‘1’ (show) — only ‘0’ means explicitly hidden if ( ‘0’ !== $show_title ) { $show_title = ‘1’; } $bottom_id = absint( get_post_meta( $post->ID, ‘_lnsel_pgl_bottom_image_id’, true ) ); $bottom_url = $bottom_id ? wp_get_attachment_image_url( $bottom_id, ‘medium’ ) : ”; if ( ! $bottom_url ) { $bottom_id = 0; } ?>
`[lnsel_gallery]` · `[lnsel_gallery layout=”grid” columns=”3″]` · `[lnsel_gallery layout=”masonry” columns=”3″]` ✕ ⋮ >
+ > /> >
<?php } /* ═══════════════════════════════════════════ 2. SAVE META ═══════════════════════════════════════════ */ add_action( 'save_post', 'lnsel_pgl_save_meta' ); function lnsel_pgl_save_meta( $post_id ) { if ( ! isset( $_POST['lnsel_pgl_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['lnsel_pgl_nonce'] ) ), 'lnsel_pgl_save_gallery' ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! current_user_can( 'edit_post', $post_id ) ) { return; } $ids = isset( $_POST['lnsel_pgl_gallery_ids'] ) ? sanitize_text_field( wp_unslash( $_POST['lnsel_pgl_gallery_ids'] ) ) : ''; $ids = preg_replace( '/[^0-9,]/', '', $ids ); update_post_meta( $post_id, '_lnsel_pgl_gallery_ids', $ids ); // Save lightbox title toggle — '1' = show, '0' = hide $show_title = isset( $_POST['lnsel_pgl_show_title'] ) ? '1' : '0'; update_post_meta( $post_id, '_lnsel_pgl_show_title', $show_title ); // Save bottom image (attachment ID, 0 = none) $bottom_id = isset( $_POST['lnsel_pgl_bottom_image_id'] ) ? absint( $_POST['lnsel_pgl_bottom_image_id'] ) : 0; if ( $bottom_id && ! wp_attachment_is_image( $bottom_id ) ) { $bottom_id = 0; } if ( $bottom_id ) { update_post_meta( $post_id, '_lnsel_pgl_bottom_image_id', $bottom_id ); } else { delete_post_meta( $post_id, '_lnsel_pgl_bottom_image_id' ); } } /* ═══════════════════════════════════════════ 3. ADMIN ASSETS — CSS + JS (meta box screen only) ═══════════════════════════════════════════ */ add_action( 'admin_enqueue_scripts', 'lnsel_pgl_admin_assets' ); function lnsel_pgl_admin_assets( $hook ) { if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) { return; } $screen = get_current_screen(); if ( ! $screen || ! in_array( $screen->post_type, lnsel_pgl_get_post_types(), true ) ) { return; } // Loads wp.media + Backbone/Underscore dependencies wp_enqueue_media(); // Admin CSS wp_enqueue_style( 'lnsel-pgl-admin-style', LNSEL_PGL_PLUGIN_URL . 'assets/css/admin.css', array(), LNSEL_PGL_VERSION ); // Admin JS — media-editor dep guarantees wp.media is available wp_enqueue_script( 'lnsel-pgl-admin-script', LNSEL_PGL_PLUGIN_URL . 'assets/js/admin.js', array( 'jquery', 'jquery-ui-sortable', 'media-editor' ), LNSEL_PGL_VERSION, true ); // Translated strings passed to JS via lnselPGLAdmin object wp_localize_script( 'lnsel-pgl-admin-script', 'lnselPGLAdmin', array( 'selectTitle' => __( 'Select Gallery Images', 'lnsel-post-gallery-lightbox' ), 'addBtn' => __( 'Add to Gallery', 'lnsel-post-gallery-lightbox' ), 'confirmClear' => __( 'Remove all gallery images from this post?', 'lnsel-post-gallery-lightbox' ), 'imgSingular' => __( 'image', 'lnsel-post-gallery-lightbox' ), 'imgPlural' => __( 'images', 'lnsel-post-gallery-lightbox' ), 'bottomTitle' => __( 'Select Bottom Image', 'lnsel-post-gallery-lightbox' ), 'bottomBtn' => __( 'Use as Bottom Image', 'lnsel-post-gallery-lightbox' ), 'bottomAdd' => __( '+ Add Bottom Image', 'lnsel-post-gallery-lightbox' ), 'bottomChange' => __( 'Change Bottom Image', 'lnsel-post-gallery-lightbox' ), 'removeLabel' => __( 'Remove', 'lnsel-post-gallery-lightbox' ), ) ); } /* ═══════════════════════════════════════════ 4. FRONT-END ASSETS ═══════════════════════════════════════════ */ add_action( 'wp_enqueue_scripts', 'lnsel_pgl_frontend_assets' ); function lnsel_pgl_frontend_assets() { // Register everywhere so the shortcode can enqueue on any page // (archives, widgets, page builders), not only singular views. wp_register_style( 'lnsel-pgl-frontend-style', LNSEL_PGL_PLUGIN_URL . 'assets/css/frontend.css', array(), LNSEL_PGL_VERSION ); wp_register_script( 'lnsel-pgl-frontend-script', LNSEL_PGL_PLUGIN_URL . 'assets/js/frontend.js', array(), LNSEL_PGL_VERSION, true ); // Load the CSS in <head> on singular views to avoid a flash of unstyled content. if ( is_singular() ) { wp_enqueue_style( 'lnsel-pgl-frontend-style' ); wp_enqueue_script( 'lnsel-pgl-frontend-script' ); } } /** * Make sure front-end assets are loaded whenever the shortcode renders. */ function lnsel_pgl_enqueue_frontend_assets() { if ( ! wp_script_is( 'lnsel-pgl-frontend-script', 'registered' ) ) { lnsel_pgl_frontend_assets(); } wp_enqueue_style( 'lnsel-pgl-frontend-style' ); wp_enqueue_script( 'lnsel-pgl-frontend-script' ); } /* ═══════════════════════════════════════════ 5. SHORTCODE [lnsel_gallery] ═══════════════════════════════════════════ */ add_shortcode( 'lnsel_gallery', 'lnsel_pgl_render_gallery' ); function lnsel_pgl_render_gallery( $atts ) { $atts = shortcode_atts( array( 'post_id' => get_the_ID(), 'layout' => 'grid', 'columns' => 3, 'size' => 'large', 'gap' => 10, 'show_title' => '', // '' = use per-post meta setting; 'yes'/'no' overrides 'show_bottom_image' => 'yes', // 'no' hides the bottom image 'bottom_size' => 'full', ), $atts, 'lnsel_gallery' ); $post_id = absint( $atts['post_id'] ); if ( ! $post_id ) return ''; $raw_ids = get_post_meta( $post_id, '_lnsel_pgl_gallery_ids', true ); if ( empty( $raw_ids ) ) return ''; $ids = array_filter( array_map( 'absint', explode( ',', $raw_ids ) ) ); if ( empty( $ids ) ) return ''; $layout = in_array( $atts['layout'], array( 'grid', 'masonry' ), true ) ? $atts['layout'] : 'grid'; $cols = max( 2, min( 4, absint( $atts['columns'] ) ) ); $size = sanitize_key( $atts['size'] ); $gap = absint( $atts['gap'] ); $section_id = wp_unique_id( 'lnsel-pgl-' . $post_id . '-' ); lnsel_pgl_enqueue_frontend_assets(); // Resolve show_title: shortcode attr overrides post meta if ( 'yes' === strtolower( $atts['show_title'] ) ) { $show_title = true; } elseif ( 'no' === strtolower( $atts['show_title'] ) ) { $show_title = false; } else { $meta = get_post_meta( $post_id, '_lnsel_pgl_show_title', true ); // Only '0' means hidden — anything else (including unset) defaults to show $show_title = ( '0' !== $meta ); } // Bottom image (optional) $bottom_html = ''; if ( 'no' !== strtolower( $atts['show_bottom_image'] ) ) { $bottom_id = absint( get_post_meta( $post_id, '_lnsel_pgl_bottom_image_id', true ) ); if ( $bottom_id ) { $bottom_html = wp_get_attachment_image( $bottom_id, sanitize_key( $atts['bottom_size'] ), false, array( 'class' => 'lnsel-pgl-bottom-img', 'loading' => 'lazy' ) ); } } ob_start(); ?> <div class="lnsel-pgl-section" id="<?php echo esc_attr( $section_id ); ?>" data-layout="<?php echo esc_attr( $layout ); ?>"> <?php if ( 'grid' === $layout ) : ?> <div class="lnsel-pgl-grid lnsel-pgl-cols-<?php echo absint( $cols ); ?>" style="gap:<?php echo absint( $gap ); ?>px;"> <?php foreach ( $ids as $i => $img_id ) : $img_id = absint( $img_id ); $full_url = wp_get_attachment_image_url( $img_id, 'full' ); $thumb_src = wp_get_attachment_image_src( $img_id, $size ); $thumb_url = $thumb_src ? $thumb_src[0] : $full_url; if ( ! $full_url ) continue; $att = get_post( $img_id ); $caption = $att ? ( ! empty( $att->post_excerpt ) ? $att->post_excerpt : $att->post_title ) : ''; $alt = get_post_meta( $img_id, '_wp_attachment_image_alt', true ); $alt = $alt ? $alt : $caption; ?> <div class="lnsel-pgl-item"> <a class="lnsel-pgl-link" href="<?php echo esc_url( $full_url ); ?>" data-full="<?php echo esc_url( $full_url ); ?>" data-caption="<?php echo esc_attr( $caption ); ?>" data-index="<?php echo absint( $i ); ?>" aria-label="<?php echo esc_attr( $alt ); ?>"> <img class="lnsel-pgl-thumb" src="<?php echo esc_url( $thumb_url ); ?>" alt="<?php echo esc_attr( $alt ); ?>" loading="lazy" /> <div class="lnsel-pgl-overlay" aria-hidden="true"> <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/><line x1="11" y1="8" x2="11" y2="14"/><line x1="8" y1="11" x2="14" y2="11"/></svg> </div> </a> </div> <?php endforeach; ?> </div> <?php else : ?> <div class="lnsel-pgl-masonry lnsel-pgl-masonry-<?php echo absint( $cols ); ?>" style="--lnsel-gap:<?php echo absint( $gap ); ?>px;--lnsel-cols:<?php echo absint( $cols ); ?>;"> <?php foreach ( $ids as $i => $img_id ) : $img_id = absint( $img_id ); $full_url = wp_get_attachment_image_url( $img_id, 'full' ); $thumb_src = wp_get_attachment_image_src( $img_id, $size ); $thumb_url = $thumb_src ? $thumb_src[0] : $full_url; if ( ! $full_url ) continue; $img_meta = wp_get_attachment_image_src( $img_id, 'full' ); $w = $img_meta ? absint( $img_meta[1] ) : 0; $h = $img_meta ? absint( $img_meta[2] ) : 0; $att = get_post( $img_id ); $caption = $att ? ( ! empty( $att->post_excerpt ) ? $att->post_excerpt : $att->post_title ) : ''; $alt = get_post_meta( $img_id, '_wp_attachment_image_alt', true ); $alt = $alt ? $alt : $caption; ?> <div class="lnsel-pgl-masonry-item"> <a class="lnsel-pgl-link" href="<?php echo esc_url( $full_url ); ?>" data-full="<?php echo esc_url( $full_url ); ?>" data-caption="<?php echo esc_attr( $caption ); ?>" data-index="<?php echo absint( $i ); ?>" aria-label="<?php echo esc_attr( $alt ); ?>"> <img class="lnsel-pgl-masonry-img" src="<?php echo esc_url( $thumb_url ); ?>" alt="<?php echo esc_attr( $alt ); ?>" loading="lazy" <?php if ( $w && $h ) : ?> width="<?php echo esc_attr( $w ); ?>" height="<?php echo esc_attr( $h ); ?>" <?php endif; ?> /> <div class="lnsel-pgl-overlay" aria-hidden="true"> <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/><line x1="11" y1="8" x2="11" y2="14"/><line x1="8" y1="11" x2="14" y2="11"/></svg> </div> </a> </div> <?php endforeach; ?> </div> <?php endif; ?> <?php if ( $bottom_html ) : ?> <div class="lnsel-pgl-bottom"><?php echo wp_kses_post( $bottom_html ); ?></div> <?php endif; ?> </div> <div class="lnsel-pgl-lightbox" id="lnsel-pgl-lb-<?php echo esc_attr( $section_id ); ?>" role="dialog" aria-modal="true" aria-label="<?php esc_attr_e( 'Image Lightbox', 'lnsel-post-gallery-lightbox' ); ?>" data-show-title="<?php echo esc_attr( $show_title ? '1' : '0' ); ?>" tabindex="-1"> <div class="lnsel-pgl-lb-bg"></div> <div class="lnsel-pgl-lb-wrap"> <button class="lnsel-pgl-lb-close" aria-label="<?php esc_attr_e( 'Close', 'lnsel-post-gallery-lightbox' ); ?>"> <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg> </button> <button class="lnsel-pgl-lb-prev" aria-label="<?php esc_attr_e( 'Previous image', 'lnsel-post-gallery-lightbox' ); ?>"> <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 18 9 12 15 6"/></svg> </button> <div class="lnsel-pgl-lb-img-wrap"> <div class="lnsel-pgl-lb-spinner" aria-hidden="true"></div> <img class="lnsel-pgl-lb-img" src="" alt="" /> </div> <button class="lnsel-pgl-lb-next" aria-label="<?php esc_attr_e( 'Next image', 'lnsel-post-gallery-lightbox' ); ?>"> <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"/></svg> </button> <div class="lnsel-pgl-lb-footer" id="lnsel-pgl-lb-footer-<?php echo esc_attr( $section_id ); ?>"> <p class="lnsel-pgl-lb-caption"></p> <span class="lnsel-pgl-lb-counter"></span> </div> </div> </div> <?php wp_add_inline_script( 'lnsel-pgl-frontend-script', 'window.lnselPGLSections = window.lnselPGLSections || []; window.lnselPGLSections.push(' . wp_json_encode( $section_id ) . ');', 'before' ); return ob_get_clean(); }
Written by Joobi from the project's own listing. See the source page
Follow this listing's changes (Atom feed)