Below, you’ll find a detailed guide on how to add the Responsive Lightbox & Gallery Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Responsive Lightbox & Gallery Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Responsive Lightbox & Gallery Plugin and the shortcodes it provides:
"Responsive Lightbox & Gallery is a WordPress plugin that enhances your site's media display. It offers a customizable, mobile-friendly lightbox effect and a versatile gallery builder for a superior user experience."
- [rl_gallery]
Responsive Lightbox & Gallery [rl_gallery] Shortcode
The Responsive Lightbox shortcode ‘rl_gallery’ generates a customizable gallery based on the provided parameters. It fetches images, applies settings, and prepares gallery shortcode parameters. It also handles gallery type, design, and lightbox data. The shortcode is versatile, allowing for frontend previews, exclusion of certain images, and forced gallery numbers. Shortcode: [rl_gallery]
Shortcode: [rl_gallery]
Parameters
Here is a list of all possible rl_gallery shortcode parameters and attributes:
id
– Specifies the unique identifier of the gallery.preview
– Controls whether to show a preview of the gallery.gallery_no
– Determines the number of the gallery to display.
Examples and Usage
Basic example – Displaying a gallery using its ID
[rl_gallery id=3 /]
Here, the ‘id’ parameter is used to specify the ID of the gallery that you want to display. The gallery with ID 3 will be displayed in this case.
Advanced examples
Displaying a gallery with a preview and specifying the gallery number
[rl_gallery id=3 preview=true gallery_no=2 /]
In this example, the ‘preview’ parameter is set to true, which means that a preview of the gallery will be displayed. The ‘gallery_no’ parameter is used to specify the number of the gallery. The gallery with ID 3 will be displayed as the second gallery.
Displaying a gallery with a specific revision ID and enabling preview
[rl_gallery id=3 rl_gallery_revision_id=5 preview=true /]
This example demonstrates how to use the ‘rl_gallery_revision_id’ parameter to specify a specific revision of the gallery to display. The ‘preview’ parameter is also set to true, so a preview of the gallery will be displayed. The gallery with ID 3 and revision ID 5 will be displayed in this case.
PHP Function Code
In case you have difficulties debugging what causing issues with [rl_gallery]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'rl_gallery', array( $this, 'gallery_shortcode' ) );
Shortcode PHP function:
function gallery_shortcode( $args ) {
// enable only for frontend previews
if ( ! is_admin() && is_preview() )
add_filter( 'get_post_metadata', array( $this, 'filter_preview_metadata' ), 10, 4 );
// prepare defaults
$defaults = [ 'id' => 0 ];
// merge defaults with arguments
$args = array_merge( $defaults, $args );
// parse id
$args['id'] = (int) $args['id'];
// is it gallery?
if ( get_post_type( $args['id'] ) !== 'rl_gallery' )
return '';
$images_args = [ 'exclude' => true ];
if ( isset( $args['preview'] ) )
$images_args['preview'] = (bool) $args['preview'];
elseif( isset( $_GET['rl_gallery_revision_id'], $_GET['preview'] ) && $_GET['preview'] === 'true' )
$images_args['preview'] = true;
else
$images_args['preview'] = false;
// get images
$images = $this->get_gallery_images( $args['id'], $images_args );
if ( ! $images )
return '';
$attachments = [];
// build config
foreach ( $images as $image ) {
if ( ! empty( $image['id'] ) )
$attachments[] = $image['id'];
}
// get config data
$config = get_post_meta( $args['id'], '_rl_config', true );
// prepare gallery shortcode parameters
$fields = [];
// get main instance
$rl = Responsive_Lightbox();
// valid menu item?
if ( ! empty( $config['menu_item'] ) ) {
// assign data from db
$data = $config[$config['menu_item']];
foreach ( $rl->frontend->get_default_gallery_fields() as $field_name => $field_args ) {
// replace default values
if ( array_key_exists( $field_name, $data ) )
$fields[$field_name] = $data[$field_name];
}
// is it default gallery type?
if ( $config['menu_item'] === 'default' ) {
// set new gallery type
$gallery_type = $rl->options['settings']['builder_gallery'];
// assign gallery settings
if ( array_key_exists( $gallery_type . '_gallery', $rl->settings->settings ) )
$gallery_fields = $rl->settings->settings[$gallery_type . '_gallery']['fields'];
// assign gallery defaults
if ( array_key_exists( $gallery_type . '_gallery', $rl->options ) )
$gallery_defaults = $rl->options[$gallery_type . '_gallery'];
} else {
$gallery_type = $config['menu_item'];
// assign gallery settings
if ( array_key_exists( $config['menu_item'] . '_gallery', $rl->settings->settings ) )
$gallery_fields = $rl->settings->settings[$config['menu_item'] . '_gallery']['fields'];
// assign gallery defaults
if ( array_key_exists( $config['menu_item'] . '_gallery', $rl->defaults ) )
$gallery_defaults = $rl->defaults[$config['menu_item'] . '_gallery'];
}
if ( isset( $gallery_fields, $gallery_defaults ) ) {
// run through all fields
foreach ( $gallery_fields as $field_name => $field_args ) {
if ( $field_args['type'] === 'multiple' ) {
foreach ( $field_args['fields'] as $subfield_name => $subfield_args ) {
// field exists in db?
if ( array_key_exists( $subfield_name, $data ) )
$fields[$subfield_name] = $data[$subfield_name];
else
$fields[$subfield_name] = $gallery_defaults[$subfield_name];
}
} else {
// field exists in db?
if ( array_key_exists( $field_name, $data ) )
$fields[$field_name] = $data[$field_name];
else
$fields[$field_name] = $gallery_defaults[$field_name];
}
}
}
// add gallery type
$fields['type'] = $gallery_type;
}
$shortcode = '';
foreach ( $fields as $arg => $value ) {
if ( is_array( $value ) )
$shortcode .= ' ' . esc_attr( $arg ) . '="' . esc_attr( (string) implode( ',', $value ) ) . '"';
else
$shortcode .= ' ' . esc_attr( $arg ) . '="' . esc_attr( (string) $value ) . '"';
}
// get design data
$design = get_post_meta( $args['id'], '_rl_design', true );
if ( ! empty( $design['menu_item'] ) ) {
$design_data = $design[$design['menu_item']];
// remove show_title to avoid shortcode attribute duplication
if ( isset( $design_data['show_title'] ) ) {
if ( ! isset( $design_data['design_show_title'] ) )
$design_data['design_show_title'] = $design_data['show_title'];
unset( $design_data['show_title'] );
}
// remove show_caption to avoid shortcode attribute duplication
if ( isset( $design_data['show_caption'] ) ) {
if ( ! isset( $design_data['design_show_caption'] ) )
$design_data['design_show_caption'] = $design_data['show_caption'];
unset( $design_data['show_caption'] );
}
foreach ( $design_data as $arg => $value ) {
$shortcode .= ' ' . esc_attr( $arg ) . '="' . esc_attr( (string) $value ) . '"';
}
}
// get lightbox data
$lightbox = get_post_meta( $args['id'], '_rl_lightbox', true );
if ( ! empty( $lightbox['menu_item'] ) ) {
foreach ( $lightbox[$lightbox['menu_item']] as $arg => $value ) {
$shortcode .= ' ' . esc_attr( $arg ) . '="' . esc_attr( (string) $value ) . '"';
}
}
$forced_gallery_no = 0;
// check forced gallery number
if ( isset( $args['gallery_no'] ) ) {
$args['gallery_no'] = (int) $args['gallery_no'];
if ( $args['gallery_no'] > 0 )
$forced_gallery_no = $args['gallery_no'];
}
// get content
$content = do_shortcode( '[gallery rl_gallery_id="' . esc_attr( $args['id'] ) .'"' . ( $forced_gallery_no > 0 ? ' rl_gallery_no="' . (int) $forced_gallery_no .'"' : '' ) . ' include="' . ( empty( $attachments ) ? '' : esc_attr( implode( ',', $attachments ) ) ) . '"' . $shortcode . ']' );
// make sure every filter is available in frontend ajax
if ( wp_doing_ajax() )
$content = $rl->frontend->add_lightbox( $content );
return $content;
}
Code file location:
responsive-lightbox/responsive-lightbox/includes/class-galleries.php
Conclusion
Now that you’ve learned how to embed the Responsive Lightbox & Gallery Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply