Gallery Shortcode

Below, you’ll find a detailed guide on how to add the 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 Gallery Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Gallery Plugin and the shortcodes it provides:

Plugin Icon
Gallery by BestWebSoft – Customizable Image and Photo Galleries for WordPress

"Gallery by BestWebSoft is a customizable plugin for WordPress. It enhances your site with image and photo galleries, offering unique visual experiences. Perfect for artists, photographers, and bloggers."

★★★★✩ (158) Active Installs: 10000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [print_gllr]

Gallery [print_gllr] Shortcode

The Gallery Plugin shortcode is designed to enhance the visual appeal of your WordPress site. It allows you to create a gallery of images and display them in a sleek and organized manner. The shortcode enables customization of image display, sorting, and categorization. It also offers features like lightbox functionality for image enlargement and a return link for easy navigation. This shortcode is a powerful tool for any site that relies heavily on visual content.

Shortcode: [print_gllr]

Parameters

Here is a list of all possible print_gllr shortcode parameters and attributes:

  • id – Specifies the unique ID of the gallery to display.
  • display – Determines the display mode of the gallery, can be ‘full’ or ‘short’.
  • cat_id – Identifies the category of the gallery to display.
  • sort_by – Defines the order in which the galleries are displayed.

Examples and Usage

Basic example – Display a gallery with a specific ID

[print_gllr id=3 /]

Advanced examples

Display a gallery with a specific ID and set the display type to ‘short’

[print_gllr id=3 display='short' /]

Display a gallery with a specific ID and sort it by a specific category ID

[print_gllr id=3 cat_id=2 /]

Display a gallery with a specific ID, sort it by a specific category ID, and set the display type to ‘full’

[print_gllr id=3 cat_id=2 display='full' /]

Display a gallery with a specific ID, sort it by a specific category ID, set the display type to ‘full’, and sort by a specific method

[print_gllr id=3 cat_id=2 display='full' sort_by='date' /]

PHP Function Code

In case you have difficulties debugging what causing issues with [print_gllr] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'print_gllr', 'gllr_shortcode' );

Shortcode PHP function:

function gllr_shortcode( $attr ) {
		global $gllr_options, $gllr_vars_for_inline_script, $gllr_plugin_info;
		if ( empty( $gllr_plugin_inf ) ) {
			if ( ! function_exists( 'get_plugin_data' ) ) {
				require_once ABSPATH . 'wp-admin/includes/plugin.php';
			}
			$gllr_plugin_info = get_plugin_data( __FILE__ );
		}
		wp_register_script( 'gllr_js', plugins_url( 'js/frontend_script.js', __FILE__ ), array( 'jquery' ), $gllr_plugin_info['Version'], true );

		extract(
			shortcode_atts(
				array(
					'id'      => '',
					'display' => 'full',
					'cat_id'  => '',
					'sort_by' => '',
				),
				$attr
			)
		);
		ob_start();
		require_once ABSPATH . 'wp-admin/includes/plugin.php';

		$galleries_order =
			( ! empty( $sort_by ) && 'default' !== $sort_by )
		?
			$sort_by
		: (
				( ! empty( $gllr_options['album_order_by_category_option'] ) && 'default' !== $gllr_options['album_order_by_category_option'] )
			?
				$gllr_options['album_order_by_category_option']
			:
				$gllr_options['album_order_by']
		);

		if ( ! empty( $cat_id ) ) {
			global $post, $wp_query;

			$term = get_term( $cat_id, 'gallery_categories' );
			if ( ! empty( $term ) ) {

				$old_wp_query = $wp_query;

				$args         = array(
					'post_type'          => $gllr_options['post_type_name'],
					'post_status'        => 'publish',
					'posts_per_page'     => -1,
					'gallery_categories' => $term->slug,
					'orderby'            => $galleries_order,
					'order'              => $gllr_options['album_order'],
				);
				$second_query = new WP_Query( $args );
				?>
				<div class="gallery_box">
					<ul>
						<?php
						if ( $second_query->have_posts() ) {
							if ( 1 === absint( $gllr_options['cover_border_images'] ) ) {
								$border = 'border-width: ' . $gllr_options['cover_border_images_width'] . 'px; border-color:' . $gllr_options['cover_border_images_color'] . ';border: ' . $gllr_options['cover_border_images_width'] . 'px solid ' . $gllr_options['cover_border_images_color'];
							} else {
								$border = '';
							}
							if ( 'album-thumb' !== $gllr_options['image_size_album'] ) {
								$width  = absint( get_option( $gllr_options['image_size_album'] . '_size_w' ) );
								$height = absint( get_option( $gllr_options['image_size_album'] . '_size_h' ) );
							} else {
								$width  = $gllr_options['custom_size_px']['album-thumb'][0];
								$height = $gllr_options['custom_size_px']['album-thumb'][1];
							}

							while ( $second_query->have_posts() ) {
								$second_query->the_post();
								$attachments = get_post_thumbnail_id( $post->ID );
								if ( empty( $attachments ) ) {
									$images_id   = get_post_meta( $post->ID, '_gallery_images', true );
									$attachments = get_posts(
										array(
											'showposts'    => 1,
											'what_to_show' => 'posts',
											'post_status'  => 'inherit',
											'post_type'    => 'attachment',
											'orderby'      => $gllr_options['order_by'],
											'order'        => $gllr_options['order'],
											'post__in'     => explode( ',', $images_id ),
											'meta_key'     => '_gallery_order_' . $post->ID,
										)
									);
									if ( ! empty( $attachments[0] ) ) {
										$first_attachment = $attachments[0];
										$image_attributes = wp_get_attachment_image_src( $first_attachment->ID, $gllr_options['image_size_album'] );
									} else {
										$image_attributes = array( '' );
									}
								} else {
									$image_attributes = wp_get_attachment_image_src( $attachments, $gllr_options['image_size_album'] );
								}
								?>
								<li>
									<a rel="bookmark" href="<?php echo esc_url( get_permalink() ); ?>" title="<?php the_title(); ?>">
										<?php
										printf(
											'<img %1$s %2$s style="%3$s %4$s %5$s" alt="%6$s" title="%6$s" src="%7$s" />',
											! empty( $width ) ? 'width="' . esc_attr( $width ) . '"' : '',
											! empty( $height ) ? 'height="' . esc_attr( $height ) . '"' : '',
											! empty( $width ) ? 'width:' . esc_attr( $width ) . 'px;' : '',
											! empty( $height ) ? 'height:' . esc_attr( $height ) . 'px;' : '',
											$border,
											esc_html( get_the_title() ),
											esc_url( $image_attributes[0] )
										);
										?>
									</a>
									<div class="gallery_detail_box">
										<div class="gllr_detail_title"><?php the_title(); ?></div>
										<div class="gllr_detail_excerpt"><?php gllr_the_excerpt_max_charlength( 100 ); ?></div>
										<a href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>"><?php echo esc_html( $gllr_options['read_more_link_text'] ); ?></a>
									</div><!-- .gallery_detail_box -->
									<div class="gllr_clear"></div>
								</li>
								<?php
							}
						}
						?>
					</ul>
				</div><!-- .gallery_box -->
				<?php
				wp_reset_postdata();
				$wp_query = $old_wp_query;
			}
		} else {
			global $post, $wp_query;
			$old_wp_query = $wp_query;

			$args         = array(
				'post_type'      => $gllr_options['post_type_name'],
				'post_status'    => 'publish',
				'p'              => $id,
				'posts_per_page' => 1,
			);
			$second_query = new WP_Query( $args );
			if ( 'short' === $display ) {
				?>
				<div class="gallery_box">
					<ul>
						<?php
						if ( $second_query->have_posts() ) {
							$second_query->the_post();
							$attachments = get_post_thumbnail_id( $post->ID );

							if ( 'album-thumb' !== $gllr_options['image_size_album'] ) {
								$width  = absint( get_option( $gllr_options['image_size_album'] . '_size_w' ) );
								$height = absint( get_option( $gllr_options['image_size_album'] . '_size_h' ) );
							} else {
								$width  = $gllr_options['custom_size_px']['album-thumb'][0];
								$height = $gllr_options['custom_size_px']['album-thumb'][1];
							}

							if ( empty( $attachments ) ) {
								$images_id   = get_post_meta( $post->ID, '_gallery_images', true );
								$attachments = get_posts(
									array(
										'showposts'      => 1,
										'what_to_show'   => 'posts',
										'post_status'    => 'inherit',
										'post_type'      => 'attachment',
										'orderby'        => $gllr_options['order_by'],
										'order'          => $gllr_options['order'],
										'post__in'       => explode( ',', $images_id ),
										'meta_key'       => '_gallery_order_' . $post->ID,
									)
								);
								if ( ! empty( $attachments[0] ) ) {
									$first_attachment = $attachments[0];
									$image_attributes = wp_get_attachment_image_src( $first_attachment->ID, $gllr_options['image_size_album'] );
								} else {
									$image_attributes = array( '' );
								}
							} else {
								$image_attributes = wp_get_attachment_image_src( $attachments, $gllr_options['image_size_album'] );
							}

							if ( 1 === absint( $gllr_options['cover_border_images'] ) ) {
								$border = 'border-width: ' . $gllr_options['cover_border_images_width'] . 'px; border-color:' . $gllr_options['cover_border_images_color'] . ';border: ' . $gllr_options['cover_border_images_width'] . 'px solid ' . $gllr_options['cover_border_images_color'];
							} else {
								$border = '';
							}
							?>
							<li>
								<a rel="bookmark" href="<?php echo esc_html( get_permalink() ); ?>" title="<?php the_title(); ?>">
									<?php
									printf(
										'<img %1$s %2$s style="%3$s %4$s %5$s" alt="%6$s" title="%6$s" src="%7$s" />',
										! empty( $width ) ? 'width="' . esc_attr( $width ) . '"' : '',
										! empty( $height ) ? 'height="' . esc_attr( $height ) . '"' : '',
										! empty( $width ) ? 'width:' . esc_attr( $width ) . 'px;' : '',
										! empty( $height ) ? 'height:' . esc_attr( $height ) . 'px;' : '',
										$border,
										esc_html( get_the_title() ),
										esc_url( $image_attributes[0] )
									);
									?>
								</a>
								<div class="gallery_detail_box">
									<div class="gllr_detail_title"><?php the_title(); ?></div>
									<div class="gllr_detail_excerpt"><?php gllr_the_excerpt_max_charlength( 100 ); ?></div>
									<a href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>"><?php echo esc_html( $gllr_options['read_more_link_text'] ); ?></a>
								</div><!-- .gallery_detail_box -->
								<div class="gllr_clear"></div>
							</li>
						<?php } ?>
					</ul>
				</div><!-- .gallery_box -->
				<?php
			} else {
				if ( $second_query->have_posts() ) {
					if ( 1 === absint( $gllr_options['border_images'] ) ) {
						$border        = 'border-width: ' . $gllr_options['border_images_width'] . 'px; border-color:' . $gllr_options['border_images_color'] . ';border: ' . $gllr_options['border_images_width'] . 'px solid ' . $gllr_options['border_images_color'];
						$border_images = $gllr_options['border_images_width'] * 2;
					} else {
						$border        = '';
						$border_images = 0;
					}
					if ( 'photo-thumb' !== $gllr_options['image_size_photo'] ) {
						$width  = absint( get_option( $gllr_options['image_size_photo'] . '_size_w' ) );
						$height = absint( get_option( $gllr_options['image_size_photo'] . '_size_h' ) );
					} else {
						$width  = $gllr_options['custom_size_px']['photo-thumb'][0];
						$height = $gllr_options['custom_size_px']['photo-thumb'][1];
					}

					while ( $second_query->have_posts() ) {
						$second_query->the_post();
						?>
						<div class="gallery_box_single">
							<?php
							echo do_shortcode( get_the_content() );

							$images_id = get_post_meta( $post->ID, '_gallery_images', true );

							$posts = get_posts(
								array(
									'showposts'      => -1,
									'what_to_show'   => 'posts',
									'post_status'    => 'inherit',
									'post_type'      => 'attachment',
									'orderby'        => $gllr_options['order_by'],
									'order'          => $gllr_options['order'],
									'post__in'       => explode( ',', $images_id ),
									'meta_key'       => '_gallery_order_' . $post->ID,
								)
							);

							if ( 0 < count( $posts ) ) {
								$count_image_block = 0;
								?>
								<div class="gallery gllr_grid" data-gllr-columns="<?php echo esc_attr( $gllr_options['custom_image_row_count'] ); ?>" data-gllr-border-width="<?php echo esc_attr( $gllr_options['border_images_width'] ); ?>">
									<?php
									foreach ( $posts as $attachment ) {
										$image_attributes       = wp_get_attachment_image_src( $attachment->ID, $gllr_options['image_size_photo'] );
										$image_attributes_large = wp_get_attachment_image_src( $attachment->ID, 'large' );
										$image_attributes_full  = wp_get_attachment_image_src( $attachment->ID, 'full' );
										$url_for_link           = get_post_meta( $attachment->ID, 'gllr_link_url', true );
										$image_text             = get_post_meta( $attachment->ID, 'gllr_image_text', true );
										$image_alt_tag          = get_post_meta( $attachment->ID, 'gllr_image_alt_tag', true );

										if ( 0 === $count_image_block % $gllr_options['custom_image_row_count'] ) {
											?>
											<div class="gllr_image_row">
										<?php } ?>
											<div class="gllr_image_block">
												<p style="
												<?php
												if ( $width ) {
													echo 'width:' . ( esc_attr( $width + $border_images ) ) . 'px;';
												} if ( $height ) {
													echo 'height:' . ( esc_attr( $height + $border_images ) ) . 'px;';
												}
												?>
												">
													<?php if ( ! empty( $url_for_link ) ) { ?>
														<a href="<?php echo esc_url( $url_for_link ); ?>" title="<?php echo esc_html( $image_text ); ?>" target="_blank">
															<?php
															printf(
																'<img %1$s %2$s style="%3$s %4$s %5$s" alt="%6$s" title="%7$s" src="%8$s" />',
																! empty( $width ) ? 'width="' . esc_attr( $width ) . '"' : '',
																! empty( $height ) ? 'height="' . esc_attr( $height ) . '"' : '',
																! empty( $width ) ? 'width:' . esc_attr( $width ) . 'px;' : '',
																! empty( $height ) ? 'height:' . esc_attr( $height ) . 'px;' : '',
																$border,
																esc_html( $image_alt_tag ),
																esc_html( $image_text ),
																esc_url( $image_attributes[0] )
															);
															?>
														</a>
														<?php
													} else {
														if ( 1 !== absint( $gllr_options['enable_image_opening'] ) ) {
															?>
															<a data-fancybox="gallery_fancybox<?php echo 0 === absint( $gllr_options['single_lightbox_for_multiple_galleries'] ) ? '_' . esc_attr( $post->ID ) : ''; ?>" href="<?php echo esc_url( $image_attributes_large[0] ); ?>" title="<?php echo esc_html( $image_text ); ?>" >
																<?php
																printf(
																	'<img %1$s %2$s style="%3$s %4$s %5$s" alt="%6$s" title="%7$s" src="%8$s" rel="%9$s" />',
																	! empty( $width ) ? 'width="' . esc_attr( $width ) . '"' : '',
																	! empty( $height ) ? 'height="' . esc_attr( $height ) . '"' : '',
																	! empty( $width ) ? 'width:' . esc_attr( $width ) . 'px;' : '',
																	! empty( $height ) ? 'height:' . esc_attr( $height ) . 'px;' : '',
																	$border,
																	esc_html( $image_alt_tag ),
																	esc_html( $image_text ),
																	esc_url( $image_attributes[0] ),
																	esc_url( $image_attributes_full[0] )
																);
																?>
															</a>
														<?php } else { ?>
															<a data-fancybox="gallery_fancybox<?php echo 0 === absint( $gllr_options['single_lightbox_for_multiple_galleries'] ) ? '_' . esc_attr( $post->ID ) : ''; ?>" href="#" style="pointer-events: none;" title="<?php echo esc_html( $image_text ); ?>" >
																<?php
																printf(
																	'<img %1$s %2$s style="%3$s %4$s %5$s" alt="%6$s" title="%7$s" src="%8$s" rel="#" />',
																	! empty( $width ) ? 'width="' . esc_attr( $width ) . '"' : '',
																	! empty( $height ) ? 'height="' . esc_attr( $height ) . '"' : '',
																	! empty( $width ) ? 'width:' . esc_attr( $width ) . 'px;' : '',
																	! empty( $height ) ? 'height:' . esc_attr( $height ) . 'px;' : '',
																	$border,
																	esc_html( $image_alt_tag ),
																	esc_html( $image_text ),
																	esc_url( $image_attributes[0] )
																);
																?>
															</a>
															<?php
														}
													}
													?>
												</p>
												<?php if ( 1 === absint( $gllr_options['image_text'] ) ) { ?>
													<div 
													<?php
													if ( $width ) {
														echo 'style="width:' . ( esc_attr( $width + $border_images ) ) . 'px;"';
													}
													?>
													class="gllr_single_image_text gllr_single_image_text_under"><?php echo esc_html( $image_text ); ?>&nbsp;</div>
												<?php } ?>
											</div><!-- .gllr_image_block -->
										<?php if ( $count_image_block % $gllr_options['custom_image_row_count'] === $gllr_options['custom_image_row_count'] - 1 ) { ?>
											</div><!-- .gllr_image_row -->
											<?php
										}
										$count_image_block++;
									}
									if ( 0 < $count_image_block && 0 !== $count_image_block % $gllr_options['custom_image_row_count'] ) {
										?>
											<div class="clear"></div>
										</div><!-- .gllr_image_row -->
									<?php } ?>
								</div><!-- .gallery.clearfix -->
								<?php
							}
							if ( 1 === absint( $gllr_options['return_link_shortcode'] ) ) {
								if ( empty( $gllr_options['return_link_url'] ) ) {
									if ( ! empty( $gllr_options['page_id_gallery_template'] ) ) {
										?>
										<div class="gllr_clear"></div>
										<div class="return_link gllr_return_link"><a href="<?php echo esc_url( get_permalink( $gllr_options['page_id_gallery_template'] ) ); ?>"><?php echo esc_html( $gllr_options['return_link_text'] ); ?></a></div>
										<?php
									}
								} else {
									?>
									<div class="gllr_clear"></div>
									<div class="return_link gllr_return_link"><a href="<?php echo esc_url( $gllr_options['return_link_url'] ); ?>"><?php echo esc_html( $gllr_options['return_link_text'] ); ?></a></div>
									<?php
								}
							}
							?>
						</div><!-- .gallery_box_single -->
						<div class="gllr_clear"></div>
						<?php
					}
					if ( $gllr_options['enable_lightbox'] ) {

						$gllr_vars_for_inline_script['single_script'][] = apply_filters( 'gllr_options_for_inline_script', array( 'post_id' => $post->ID ) );

						if ( defined( 'BWS_ENQUEUE_ALL_SCRIPTS' ) ) {
							gllr_echo_inline_script();
						}
					}
				} else {
					?>
					<div class="gallery_box_single">
						<p class="not_found"><?php esc_html_e( 'Sorry, nothing found.', 'gallery-plugin' ); ?></p>
					</div><!-- .gallery_box_single -->
					<?php
				}
			}
			wp_reset_postdata();
			$wp_query = $old_wp_query;
		}
		$gllr_output = ob_get_clean();
		return $gllr_output;
	}

Code file location:

gallery-plugin/gallery-plugin/gallery-plugin.php

Conclusion

Now that you’ve learned how to embed the 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.

Comments

Leave a Reply

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