MaxGalleria Shortcodes

Below, you’ll find a detailed guide on how to add the MaxGalleria Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the MaxGalleria Plugin shortcodes not to show or not to work correctly.

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

Plugin Icon
MaxGalleria

"MaxGalleria is a powerful WordPress plugin that helps you create stunning image and video galleries with ease. Its user-friendly interface makes gallery management a breeze. Perfect for photographers and designers."

★★★★✩ (61) Active Installs: 3000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [maxgallery_thumb]
  • [maxgallery]

MaxGalleria [maxgallery_thumb] Shortcode

The MaxGalleria plugin shortcode, ‘maxgallery_thumb’, is designed to display a thumbnail gallery. It accepts parameters like ‘id’, ‘name’, ‘size’, ‘width’, ‘height’, ‘url’, and ‘target’. It fetches the gallery post based on ‘id’ or ‘name’ and returns the output after sanitizing the input parameters. If the gallery post status is ‘publish’, it generates the gallery output.

Shortcode: [maxgallery_thumb]

Parameters

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

  • id – The unique identifier of the gallery
  • name – The specific name of the gallery
  • size – Adjusts the size of the gallery thumbnail
  • width – Sets the width of the gallery thumbnail
  • height – Defines the height of the gallery thumbnail
  • url – Specifies the URL where the thumbnail links to
  • target – Determines where the linked URL will open

Examples and Usage

Basic example – Displays the MaxGalleria gallery thumbnail by referencing the gallery ID.

[maxgallery_thumb id="123" /]

Advanced example – Displays the MaxGalleria gallery thumbnail by referencing the gallery name, with a specified size, width, height, URL, and target.

[maxgallery_thumb name="my-gallery" size="thumbnail" width="200" height="200" url="http://example.com" target="_blank" /]

Another advanced example – Using the shortcode to display a MaxGalleria gallery thumbnail by referencing both ID and name. The gallery will first try to load by ID, but if not found, it will try to load by name.

[maxgallery_thumb id="123" name="my-gallery" /]

Please note that the ‘id’ parameter takes precedence over the ‘name’ parameter. If both are provided, the gallery with the specified ID will be displayed, regardless of the specified name.

PHP Function Code

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

Shortcode line:

add_shortcode('maxgallery_thumb', array($this, 'maxgallery_thumb_shortcode'));

Shortcode PHP function:

                    function maxgallery_thumb_shortcode($atts) {	
		extract(shortcode_atts(array(
			'id' => '',
			'name' => '',
			'size' => '',
			'width' => '',
			'height' => '',
			'url' => '',
			'target' => ''
		), $atts));
		
		$gallery_id = sanitize_text_field("{$id}");
		$gallery_name = sanitize_text_field("{$name}");
		
		$output = '';
		$gallery = null;
		
		if ($gallery_id != '' && $gallery_name != '') {
			// If both given, the id wins
			$gallery = get_post($gallery_id);
		}

		if ($gallery_id != '' && $gallery_name == '') {
			// Get the gallery by id
			$gallery = get_post($gallery_id);
		}
		
		if ($gallery_id == '' && $gallery_name != '') {
			// Get the gallery by name
			$query = new WP_Query(array('name' => $gallery_name, 'post_type' => MAXGALLERIA_POST_TYPE));
			$gallery = $query->get_queried_object();
		}
		
		if (isset($gallery) && $gallery->post_status == 'publish') {
			$attrs = array(
				'size' => sanitize_text_field("{$size}"),
				'width' => sanitize_text_field("{$width}"),
				'height' => sanitize_text_field("{$height}"),
				'url' => sanitize_text_field("{$url}"),
				'target' => sanitize_text_field("{$target}")
			);
		
			$output = $this->get_output($gallery, $attrs);
		}
		
		return $output;
	}
                    

Code file location:

maxgalleria/maxgalleria/maxgalleria-shortcode-thumb.php

MaxGalleria [maxgallery] Shortcode

The Maxgalleria shortcode is used to generate and display a gallery based on the ID or name provided. It facilitates sorting by order or type and paginates the gallery if needed.

Shortcode: [maxgallery]

Parameters

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

  • id – Unique identifier for a specific gallery
  • name – The name assigned to the gallery

Examples and Usage

Basic example – Display a gallery using its ID

[maxgallery id="2" /]

Advanced example – Display a gallery using both ID and name. The gallery will first try to load by ID, but if not found, it will try to load by name.

[maxgallery id="2" name="my-gallery" /]

Advanced example – Display a gallery using its name. This could be useful if you don’t remember the ID of the gallery, but you know its name.

[maxgallery name="my-gallery" /]

PHP Function Code

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

Shortcode line:

add_shortcode('maxgallery', array($this, 'maxgallery_shortcode'));

Shortcode PHP function:

                    function maxgallery_shortcode($atts) {	
    
    global $authordata;
    $authortemp = $authordata;
    $items_per_page = -1;
    $sort_by = 'menu_order';
    $sort_order = 'desc';
    
		extract(shortcode_atts(array(
			'id' => '',
			'name' => ''
		), $atts));
		
		$gallery_id = sanitize_text_field("{$id}");
		$gallery_name = sanitize_text_field("{$name}");
		
		$output = '';
		$gallery = null;
    $total_posts = 1;
    		
		if ($gallery_id != '' && $gallery_name != '') {
			// If both given, the id wins
			$gallery = get_post($gallery_id);
		}

		if ($gallery_id != '' && $gallery_name == '') {
			// Get the gallery by id
			$gallery = get_post($gallery_id);
		}
		
		if ($gallery_id == '' && $gallery_name != '') {
			// Get the gallery by name
			$query = new WP_Query(array('name' => $gallery_name, 'post_type' => MAXGALLERIA_POST_TYPE));
			$gallery = $query->get_queried_object();
		}
    		
		if (isset($gallery) && $gallery->post_status == 'publish') {
        
      global $wpdb;
      
      $options = new MaxGalleryOptions($gallery->ID);
			$template_key = $options->get_template();
			            
      if($options->is_video_gallery()) {
        
				switch($template_key) {
        
					case 'video-tiles':
        
            $items_per_page = get_post_meta( $gallery->ID, 'maxgallery_videos_per_page', true );

            $sort_order = trim(get_post_meta( $gallery->ID, 'maxgallery_sort_order_video_tiles', true ));
            if($sort_order == '')
              $sort_order = get_option('maxgallery_sort_order_image_video_default', 'asc');

            $sort_by = trim(get_post_meta( $gallery->ID, 'maxgallery_sort_type_video_tiles', true ));
            if($sort_by == '')
              $sort_by = get_option('maxgallery_sort_type_image_video_default', 'asc');
          
            break;
          
					case 'video-showcase':
            $items_per_page = 0;
            
            $sort_order = trim(get_post_meta( $gallery->ID, 'maxgallery_sort_order_video_sc', true ));
            if($sort_order == '')
              $sort_order = get_option('maxgallery_sort_order_video_sc_default', 'asc');

            $sort_by = trim(get_post_meta( $gallery->ID, 'maxgallery_sort_type_video_sc', true ));
            if($sort_by == '')
              $sort_by = get_option('maxgallery_sort_type_video_sc_default', 'asc');
            
            break;
          
          case 'slick-slider':
					  $items_per_page = 0;
            $sort_by = 'menu_order';
            $sort_order = 'asc';
            break;
          
          default:
					  $items_per_page = 0;
            break;
          
        }  
                  
      } else {
        
				switch($template_key) {
					case 'image-tiles':
            $items_per_page = get_post_meta( $gallery->ID, 'maxgallery_images_per_page', true );
            
            $sort_order = trim(get_post_meta( $gallery->ID, 'maxgallery_sort_order_image_tiles', true ));
            if($sort_order == '')
              $sort_order = get_option('maxgallery_sort_order_image_tiles_default', 'asc');

            $sort_by = trim(get_post_meta( $gallery->ID, 'maxgallery_sort_type_image_tiles', true ));
            if($sort_by == '')
              $sort_by = get_option('maxgallery_sort_type_image_tiles_default', 'asc');
            
						break;            
            
					case 'image-showcase':
            $items_per_page = 0;
            
            $sort_order = trim(get_post_meta( $gallery->ID, 'maxgallery_sort_order_image_sc', true ));
            if($sort_order == '')
              $sort_order = get_option('maxgallery_sort_order_image_sc_default', 'asc');

            $sort_by = trim(get_post_meta( $gallery->ID, 'maxgallery_sort_type_image_sc', true ));
            if($sort_by == '')
              $sort_by = get_option('maxgallery_sort_type_image_sc_default', 'asc');
            
						break;
            					
					case 'masonry':
            $items_per_page = get_post_meta( $gallery->ID, 'maxgallery_masonry_images_per_page', true );
						break;
					
					default:
						$items_per_page = 0;
            $sort_order = 'asc';
						break;
					
				}
        
      }
                  
      if(($items_per_page == '') || ($items_per_page == 0)) {
        // if blank, get all attachments by setting items_per_page to -1
        $items_per_page = -1;
      }   
      else { 
        // calculate total number of pages; for this we need the total number of attachments
        $sql = "select SQL_CALC_FOUND_ROWS ID from " . $wpdb->prefix . "posts where post_parent = $gallery->ID and post_type = 'attachment'";
        $rows = $wpdb->get_results($sql);
        $count = $wpdb->get_row("select FOUND_ROWS()", ARRAY_A);
        $total_posts = $count['FOUND_ROWS()'];
      }
      
      $paged = (get_query_var('mg_page')) ? get_query_var('mg_page') : 1;      
			$args = array(
				'post_parent' => $gallery->ID,
				'post_type' => 'attachment',
				'orderby' => $sort_by,
				'order' => $sort_order,
				'numberposts' => -1, // All of them
        'posts_per_page' => $items_per_page,
        'paged' => $paged
			);

			$attachments = get_posts($args);
   
      $total_number_pages = ceil($total_posts / $items_per_page);
     
      foreach ($attachments as $attachment) {
        setup_postdata($attachment);
			
			if (count($attachments) > 0) {
				//$options = new MaxGalleryOptions($gallery->ID);

				global $maxgalleria;
				$templates = $maxgalleria->get_template_addons();
				
				foreach ($templates as $template) {
					if ($template['key'] == $options->get_template()) {
						$output = call_user_func($template['output'], $gallery, $attachments);
					}
				}
			}
		}
      }
    
    
   
    // display page links if we have pages
    if($items_per_page != -1) {
        $link = get_the_permalink();
        $link = rtrim($link, '/') . '/'; // remove the slash (if present) and then add back.
        $prev = $paged-1;
        $next = $paged+1;
        $page_links  = "<div style='clear:both'></div><div class='mg-pagination'>";
        if($prev != 0) {
            $page_links .= '<a class="mg-page-previous" href="'.$link.'?mg_page='.($paged-1).'">&laquo; Previous</a>'; 
        }
        if($next <= $total_number_pages) {
            $page_links .= '<a class="mg-page-next" href="'.$link.'?mg_page='.($paged+1).'">Next &raquo;</a>'; 
        }
        $page_links .= "</div>";    
    }
    else
        $page_links = "";
    
	  $authordata = $authortemp;
      
	return $output . $page_links;
	}
                    

Code file location:

maxgalleria/maxgalleria/maxgalleria-shortcode.php

Conclusion

Now that you’ve learned how to embed the MaxGalleria Plugin shortcodes, 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 *