Download Monitor Shortcodes

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

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

Plugin Icon
Download Monitor

"Download Monitor is a WordPress plugin designed to manage and track downloadable files on your website. It provides a user-friendly interface for uploading, customizing, and downloading digital content."

★★★★✩ (461) Active Installs: 100000+ Tested with: 6.3.2 PHP Version: 5.6
Included Shortcodes:
  • [dlm_buy]
  • [total_downloads]
  • [total_files]
  • [download]
  • [download_data]
  • [downloads]
  • [dlm_no_access]

Download Monitor [dlm_buy] Shortcode

The Download Monitor shortcode creates a dynamic link to a specified product’s “Add to Cart” page. It extracts the product ID from the shortcode attributes, generates the product’s URL, and wraps it around the provided content. If no content is provided, it retrieves a default “Add to Cart” button template. If the product ID is invalid or missing, it outputs a “Product not found” message. The shortcode also supports automatic paragraph formatting.

Shortcode: [dlm_buy]

Parameters

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

  • id – The unique identifier of the product
  • autop – If set to true, automatically adds paragraph tags
  • template – Specifies the template part to be loaded

Examples and Usage

Basic example – A simple shortcode usage to display a product with a specified ID

[dlm_buy id=101 /]

Advanced examples

Here, the shortcode is used to display a product with a specified ID and a custom template. The ‘template’ attribute allows you to apply a custom layout to the product display.

[dlm_buy id=102 template="custom_template" /]

In this example, the shortcode is used to display a product with a specified ID and the content is automatically wrapped in paragraph tags. The ‘autop’ attribute is set to ‘true’, meaning the output will be automatically wrapped in <p>…</p> HTML tags.

[dlm_buy id=103 autop="true" /]

This example shows the shortcode being used to display a product with a specified ID, a custom template, and the content is automatically wrapped in paragraph tags. This is a combination of the previous examples, showcasing the flexibility of the shortcode.

[dlm_buy id=104 template="custom_template" autop="true" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'dlm_buy', array( $this, 'content' ) );

Shortcode PHP function:

function content( $atts, $content = '' ) {

		// extract shortcode atts
		extract( shortcode_atts( array(
			'id'       => '',
			'autop'    => false,
			'template' => ''
		), $atts ) );

		// Check id
		if ( empty( $id ) ) {
			return "";
		}

		// shortcode output
		$output = '';

		// create download object
		try {
			/** @var \WPChill\DownloadMonitor\Shop\Product\Product $download */
			$product = Services::get()->service( 'product_repository' )->retrieve_single( $id );

			$atc_url = Services::get()->service( 'page' )->get_add_to_cart_url( $product->get_id() );

			// if we have content, wrap in a link only
			if ( $content ) {
				$output = '<a href="' . $atc_url . '">' . $content . '</a>';
			} else {

				// buffer
				ob_start();

				// load template
				download_monitor()->service( 'template_handler' )->get_template_part( 'shop/button/add-to-cart', $template, '', array(
					'product' => $product,
					'atc_url' => $atc_url
				) );

				// get output
				$output = ob_get_clean();

				// check if we need to wpautop()
				if ( 'true' === $autop || true === $autop ) {
					$output = wpautop( $output );
				}
			}
		} catch ( \Exception $e ) {
			$output = '[' . __( 'Product not found', 'download-monitor' ) . ']';
		}

		return $output;

	}

Code file location:

download-monitor/download-monitor/src/Shop/Shortcode/Buy.php

Download Monitor [total_downloads] Shortcode

The Download Monitor plugin shortcode ‘total_downloads’ is used to display the total number of downloads from your website. This shortcode executes a function that first checks if the download log table exists in the database. If it does, it fetches the total download count of all published posts. If the table doesn’t exist or logging is disabled, it returns an error message.

Shortcode: [total_downloads]

Examples and Usage

Basic example – Displays the total number of downloads for all published posts.

[total_downloads /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'total_downloads', array( $this, 'total_downloads' ) );

Shortcode PHP function:

function total_downloads() {

		global $wpdb;

		$total = 0;

		if ( DLM_Utils::table_checker( $wpdb->dlm_downloads ) ) {
			$total = $wpdb->get_results( "SELECT downloads.download_count FROM {$wpdb->dlm_downloads} downloads INNER JOIN {$wpdb->posts} posts ON downloads.download_id = posts.ID WHERE 1=1 AND posts.post_status = 'publish';",ARRAY_A );
			$total = array_sum( array_column( $total, 'download_count' ) );
		} else {

			if ( ! DLM_Logging::is_logging_enabled() ) {

				return esc_html__( 'Logging is not enabled.', 'download-monitor' );
			}

			return esc_html__( 'Log table not present.', 'download-monitor' );

		}

		return apply_filters( 'dlm_shortcode_total_downloads', $total );
	}

Code file location:

download-monitor/download-monitor/src/Shortcodes.php

Download Monitor [total_files] Shortcode

The Download Monitor shortcode ‘total_files’ retrieves the total number of published downloads. It uses the wp_count_posts function to count the posts of type ‘dlm_download’.

Shortcode: [total_files]

Examples and Usage

Basic example – This shortcode displays the total count of published files in the download monitor plugin.

[total_files /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'total_files', array( $this, 'total_files' ) );

Shortcode PHP function:

function total_files() {
		$count_posts = wp_count_posts( 'dlm_download' );

		return $count_posts->publish;
	}

Code file location:

download-monitor/download-monitor/src/Shortcodes.php

Download Monitor [download] Shortcode

The Download Monitor shortcode is a versatile tool that enables file downloads on your website. It extracts shortcode attributes, enqueues styles, and allows the addition of extra scripts. It checks the download ID and allows third-party extensions to manipulate the shortcode. If a specific version is set, it retrieves that version. The shortcode wraps content in a download link or loads a template if no content exists. Shortcode: [download]

Shortcode: [download]

Parameters

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

  • id – The unique identifier of the download file.
  • autop – Boolean parameter to enable automatic paragraph formatting.
  • template – The template used for the download display.
  • version_id – Specific version identifier of the download file.
  • version – The version name of the download file.

Examples and Usage

Basic example – The shortcode displays the download link for a specific download item by using the ID of the download.

[download id=5 /]

Advanced examples

Display the download link with custom content. The content is wrapped in the download link.

[download id=5]Click here to download[/download]

Specify a specific version of the download using the version parameter. If the version does not exist, it will default to the latest version.

[download id=5 version="1.0.1" /]

Use the autop parameter to automatically add paragraph tags around the download link. This can be useful for formatting the output in your posts or pages.

[download id=5 autop=true /]

Specify a custom download template to display the download link. The template parameter accepts the name of a custom template file located in your theme’s directory.

[download id=5 template="custom-download-template" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'download', array( $this, 'download' ) );

Shortcode PHP function:

function download( $atts, $content = '' ) {
		// enqueue style only on shortcode use
		wp_enqueue_style( 'dlm-frontend' );

		// Action to allow the adition of extra scripts and code related to the shortcode
		do_action( 'dlm_download_shortcode_scripts' );

		// extract shortcode atts
		extract( shortcode_atts( array(
			'id'         => '',
			'autop'      => false,
			'template'   => dlm_get_default_download_template(),
			'version_id' => null,
			'version'    => ''
		), $atts ) );


		// Make id filterable
		$id = apply_filters( 'dlm_shortcode_download_id', $id, $atts );

		// Check id
		if ( empty( $id ) ) {
			return "";
		}

		// Allow third party extensions to hijack shortcode
		$hijacked_content = apply_filters( 'dlm_shortcode_download_content', '', $id, $atts, $content );

		// If there's hijacked content, return it and be done with it
		if ( '' !== $hijacked_content ) {
			return $hijacked_content;
		}

		// shortcode output
		$output = '';

		// create download object
		try {
			/** @var DLM_Download $download */
			$download = download_monitor()->service( 'download_repository' )->retrieve_single( $id );

			// check if version is set
			if ( ! empty( $version ) ) {
				$version_id = $download->get_version_id_version_name( $version );
			}

			// check if version ID is set
			if ( isset( $version_id ) && 0 != $version_id ) {
				try {
					$version = download_monitor()->service( 'version_repository' )->retrieve_single( $version_id );
					$download->set_version( $version );
				} catch ( Exception $e ) {
				}
			}

			// if we have content, wrap in a link only
			if ( $content ) {
				$output = '<a href="' . $download->get_the_download_link() . '">' . $content . '</a>';
			} else {
				// template handler
				$template_handler = new DLM_Template_Handler();

				// buffer
				ob_start();

				// load template
				if( $download ) {
					$template_handler->get_template_part( 'content-download', $template, '', array( 'dlm_download' => $download ) );
				} else {
					echo esc_html__( 'No download defined', 'download-monitor' );
				}

				// get output
				$output .= ob_get_clean();
				// check if we need to wpautop()
				if ( 'true' === $autop || true === $autop ) {
					$output = wpautop( $output );
				}
			}
		} catch ( Exception $e ) {
			$output = '[' . __( 'Download not found', 'download-monitor' ) . ']';
		}

		return $output;
	}

Code file location:

download-monitor/download-monitor/src/Shortcodes.php

Download Monitor [download_data] Shortcode

The Download Monitor shortcode retrieves and displays various data related to a specific download. It extracts attributes like ‘id’, ‘data’, ‘version_id’, and ‘version’. It fetches the download from the repository based on the ‘id’. If ‘version’ or ‘version_id’ are provided, it fetches that specific version. It returns different data based on the ‘data’ attribute, such as filename, filetype, filesize, and more. The shortcode also fetches and displays download information like title, short description, download link, download count, and post content. It can return the download’s image and thumbnail, and list its tags and categories. If the download is not found, it returns a ‘Download not found’ message.

Shortcode: [download_data]

Parameters

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

  • id – Specifies the unique identifier of the download.
  • data – Defines the type of data to be returned.
  • version_id – Used to specify a particular version of the download.
  • version – Determines the version number of the download.

Examples and Usage

Basic example – Displaying the download link of a specific download by referencing its ID.

[download_data id="123" data="download_link" /]

Advanced examples

Displaying the file type and file size of a specific download version by referencing its ID and version.

[download_data id="123" data="filetype" version="1.0.0" /]

Displaying the title and short description of a specific download by referencing its ID.

[download_data id="123" data="title" /]
[download_data id="123" data="short_description" /]

Displaying the download count and the author of a specific download by referencing its ID.

[download_data id="123" data="download_count" /]
[download_data id="123" data="author" /]

Displaying the tags and categories of a specific download by referencing its ID.

[download_data id="123" data="tags" /]
[download_data id="123" data="categories" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'download_data', array( $this, 'download_data' ) );

Shortcode PHP function:

function download_data( $atts ) {

		// Action to allow the adition of extra scripts and code related to the shortcode
		do_action( 'dlm_download_data_shortcode_scripts' );

		extract( shortcode_atts( array(
			'id'         => '',
			'data'       => '',
			'version_id' => null,
			'version'    => ''
		), $atts ) );

		$id = apply_filters( 'dlm_shortcode_download_id', $id, $atts );

		if ( empty( $id ) || empty( $data ) ) {
			return "";
		}

		try {
			/** @var DLM_Download $download */
			$download = download_monitor()->service( 'download_repository' )->retrieve_single( $id );

			if ( ! empty( $version ) ) {
				$version_id = $download->get_version_id_version_name( $version );
			}

			if ( ! empty( $version_id ) ) {
				try {
					$version = download_monitor()->service( 'version_repository' )->retrieve_single( $version_id );
					$download->set_version( $version );
				} catch ( Exception $e ) {

				}
			}

			switch ( $data ) {

				// File / Version Info
				case 'filename' :
					return $download->get_version()->get_filename();
				case 'filetype' :
					return $download->get_version()->get_filetype();
				case 'filesize' :
					return $download->get_version()->get_filesize_formatted();
				case 'md5' :
					return $download->get_version()->get_md5();
				case 'sha1' :
					return $download->get_version()->get_sha1();
				case 'sha256' :
					return $download->get_version()->get_sha256();
				case 'crc32' :
				case 'crc32b' :
					return $download->get_version()->get_crc32b();
				case 'version' :
					return $download->get_version()->get_version_number();

				// Download Info
				case 'title' :
					return $download->get_title();
				case 'short_description' :
					return wpautop( wptexturize( do_shortcode( $download->get_excerpt() ) ) );
				case 'download_link' :
					return $download->get_the_download_link();
				case 'download_count' :
					return $download->get_download_count();
				case 'post_content' :
					return wpautop( wptexturize( do_shortcode( $download->get_description() ) ) );
				case 'post_date' :
				case 'file_date' :
					return date_i18n( get_option( 'date_format' ), $download->get_version()->get_date()->format( 'U' ) );
				case 'author' :
					return $download->get_the_author();

				// Images
				case 'image' :
					return $download->get_image( 'full' );
				case 'thumbnail' :
					return $download->get_image( 'thumbnail' );

				// Taxonomies
				case 'tags' :
					$returnstr = "";
					$terms     = get_the_terms( $id, 'dlm_download_tag' );
					if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
						$terms_names = array();
						foreach ( $terms as $term ) {
							$terms_names[] = $term->name;
						}
						$returnstr = implode( ", ", $terms_names );
					}

					return $returnstr;
				case 'categories' :
					$returnstr = "";
					$terms     = get_the_terms( $id, 'dlm_download_category' );
					if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
						$terms_names = array();
						foreach ( $terms as $term ) {
							$terms_names[] = $term->name;
						}
						$returnstr = implode( ", ", $terms_names );
					}

					return $returnstr;

			}
		} catch ( Exception $e ) {
			return '[' . __( 'Download not found', 'download-monitor' ) . ']';
		}

	}

Code file location:

download-monitor/download-monitor/src/Shortcodes.php

Download Monitor [downloads] Shortcode

The Download Monitor shortcode is used to display a list of downloadable files on a WordPress site. This shortcode accepts various parameters such as ‘per_page’, ‘orderby’, ‘order’, ‘include’, ‘exclude’, and more to customize the output. The ‘downloads’ function processes these parameters, queries the database for matching downloads, and formats the output in a list. The shortcode also supports pagination and can be used to display only featured or members-only downloads. It can filter downloads by category or tag, and exclude specific downloads by ID or tag. Finally, the output of the shortcode can be customized with a template, and the start and end of the loop, as well as the HTML before and after each download, can be defined.

Shortcode: [downloads]

Parameters

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

  • per_page – Define how many downloads to show per page
  • orderby – Set the order of the downloads by title, date, etc.
  • order – Sort downloads in ascending or descending order
  • include – Include specific downloads by their IDs
  • exclude – Exclude specific downloads by their IDs
  • offset – Skip a certain number of downloads in the output
  • category – Show downloads from specific categories
  • category_include_children – Decide whether to include downloads from child categories
  • tag – Show downloads with specific tags
  • exclude_tag – Exclude downloads with specific tags
  • featured – Only show downloads marked as featured
  • members_only – Show downloads only available to members
  • template – Choose the template for displaying downloads
  • loop_start – Define the HTML to start the download loop
  • loop_end – Define the HTML to end the download loop
  • before – Define the HTML before each download
  • after – Define the HTML after each download
  • paginate – Enable or disable pagination

Examples and Usage

Basic example – Display all downloads without any limit or specific order.

[downloads per_page="-1" /]

Advanced examples

Display downloads from a specific category, ordered by download count in descending order, and limit the number of downloads to 10.

[downloads category="ebooks" orderby="download_count" order="desc" per_page="10" /]

Display only featured downloads, ordered by title in ascending order, and exclude downloads with specific IDs.

[downloads featured="true" orderby="title" order="asc" exclude="5,6,7" /]

Display downloads that are tagged with a specific tag, ordered by date in descending order, and limit the number of downloads to 5.

[downloads tag="premium" orderby="date" order="desc" per_page="5" /]

Display downloads that are only available to members, ordered by date in ascending order, and limit the number of downloads to 10.

[downloads members_only="true" orderby="date" order="asc" per_page="10" /]

Display downloads from multiple categories (using AND relation), ordered by modified date in descending order, and limit the number of downloads to 5.

[downloads category="ebooks+audiobooks" orderby="modified" order="desc" per_page="5" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'downloads', array( $this, 'downloads' ) );

Shortcode PHP function:

function downloads( $atts ) {
		global $dlm_max_num_pages;

		// Action to allow the adition of extra scripts and code related to the shortcode
		do_action( 'dlm_downloads_shortcode_scripts' );

		// enqueue style only on shortcode use
		wp_enqueue_style( 'dlm-frontend' );
		extract( shortcode_atts( array(
			// Query args
			'per_page'                  => '-1', // -1 = no limit
			'orderby'                   => 'date', // title, rand, ID, none, date, modifed, post__in, download_count
			'order'                     => 'desc', // ASC or DESC
			'include'                   => '', // Comma separate IDS
			'exclude'                   => '', // Comma separate IDS
			'offset'                    => '',
			'category'                  => '', // Comma separate slugs
			'category_include_children' => true, // Set to false to not include child categories
			'tag'                       => '', // Comma separate slugs
			'exclude_tag'               => '', // Comma separate slugs
			'featured'                  => false, // Set to true to only pull featured downloads
			'members_only'              => false, // Set to true to only pull member downloads

			// Output args
			'template'                  => dlm_get_default_download_template(),
			'loop_start'                => '<ul class="dlm-downloads">',
			'loop_end'                  => '</ul>',
			'before'                    => '<li>',
			'after'                     => '</li>',
			'paginate'                  => false
		), $atts ) );

		$post__in       = ! empty( $include ) ? explode( ',', $include ) : '';
		$post__not_in   = ! empty( $exclude ) ? explode( ',', $exclude ) : '';
		$tag_not_in     = ! empty( $exclude_tag ) ? explode( ',', $exclude_tag ) : '';
		$order          = strtoupper( $order );
		$meta_key       = '';
		$order_by_count = '';

		switch ( $orderby ) {
			case 'title' :
			case 'rand' :
			case 'ID' :
			case 'date' :
			case 'modified' :
			case 'post__in' :
			case 'menu_order' :
				$orderby = $orderby;
				break;
			case 'id' :
				$orderby = 'ID';
				break;
			case 'hits' :
			case 'count' :
			case 'download_count' :
				$order_by_count = '1';
				break;
			default :
				$orderby = 'title';
				break;
		}

		$args = array(
			'post_type'      => 'dlm_download',
			'post_status'    => 'publish',
			'orderby'        => $orderby,
			'order'          => $order,
			'meta_key'       => $meta_key,
			'post__in'       => $post__in,
			'post__not_in'   => $post__not_in,
			'meta_query'     => array(),
			'order_by_count' => $order_by_count,
		);

		if ( $category || $tag || ! empty( $tag_not_in ) ) {
			$args['tax_query'] = array( 'relation' => 'AND' );

			$tags = array_filter( explode( ',', $tag ) );

			// check if we include category children
			$include_children = ( $category_include_children === 'true' || $category_include_children === true );

			if ( ! empty( $category ) ) {

				if ( preg_match( '/\+/', $category ) ) {

					// categories with AND

					// string to array
					$categories = array_filter( explode( '+', $category ) );

					// check if explode had results
					if ( ! empty( $categories ) ) {

						foreach ( $categories as $category ) {
							$args['tax_query'][] = array(
								'taxonomy'         => 'dlm_download_category',
								'field'            => 'slug',
								'terms'            => $category,
								'include_children' => $include_children
							);
						}

					}

				} else {

					// categories with OR

					// string to array
					$categories = array_filter( explode( ',', $category ) );

					// check if explode had results
					if ( ! empty( $categories ) ) {

						$args['tax_query'][] = array(
							'taxonomy'         => 'dlm_download_category',
							'field'            => 'slug',
							'terms'            => $categories,
							'include_children' => $include_children
						);

					}

				}

			}

			if ( ! empty( $tags ) ) {
				$args['tax_query'][] = array(
					'taxonomy' => 'dlm_download_tag',
					'field'    => 'slug',
					'terms'    => $tags
				);
			}

			if ( ! empty( $tag_not_in ) ) {
				$args['tax_query'][] = array(
					'taxonomy' => 'dlm_download_tag',
					'field'    => 'slug',
					'terms'    => $tag_not_in,
					'operator' => 'NOT IN'
				);
			}
		}

		if ( $featured === 'true' || $featured === true ) {
			$args['meta_query'][] = array(
				'key'   => '_featured',
				'value' => 'yes'
			);
		} else if ( $featured === 'exclude_featured' ) {
			$args['meta_query'][] = array(
				'key'   => '_featured',
				'value' => 'no'
			);
		}

		if ( $members_only === 'true' || $members_only === true ) {
			$args['meta_query'][] = array(
				'key'   => '_members_only',
				'value' => 'yes'
			);
		}

		ob_start();

		// Allow filtering of arguments
		$args = apply_filters( 'dlm_shortcode_downloads_args', $args, $atts );

		$offset = $paginate ? ( max( 1, get_query_var( 'paged' ) ) - 1 ) * $per_page : $offset;

		// set offset to 0 if empty
		if ( '' === $offset ) {
			$offset = 0;
		}

		// fetch downloads
		$downloads   = download_monitor()->service( 'download_repository' )->retrieve( $args, $per_page, $offset );

		// make all downloads filterable
		$downloads = apply_filters( 'dlm_shortcode_downloads_downloads', $downloads );

		// only calculate pages if we're paginating. Saves us a query when we're not
		$pages = 1;
		if ( $paginate ) {
			$pages = ceil( download_monitor()->service( 'download_repository' )->num_rows( $args ) / $per_page );
		}

		// Template handler
		$template_handler = new DLM_Template_Handler();

		if ( count( $downloads ) > 0 ) {

			// loop start output
			echo wp_kses_post( html_entity_decode( $loop_start ) );

			foreach ( $downloads as $download ) {

				// make download filterable
				$download = apply_filters( 'dlm_shortcode_downloads_loop_download', $download );

				// check if filtered download is still a DLM_Download instance
				if ( ! $download instanceof DLM_Download ) {
					continue;
				}

				// display the 'before'
				echo wp_kses_post( html_entity_decode( $before ) );

				// Allow third party extensions to hijack shortcode
				$hijacked_content = apply_filters( 'dlm_shortcode_downloads_download_content', '', $download, $atts );

				// If there's hijacked content, return it and be done with it.
				if ( '' !== $hijacked_content ) {
					echo wp_kses_post( $hijacked_content );
				} else {
					// load the template
					if ( $download->has_version() ) {
						$template_handler->get_template_part( 'content-download', $template, '', array( 'dlm_download' => $download ) );
					} else {
						$template_handler->get_template_part( 'content-download', 'no-version', '', array( 'dlm_download' => $download ) );
					}
				}

				// display the 'after'
				echo wp_kses_post( html_entity_decode( $after ) );

			} // end of the loop.

			// end of loop html
			echo wp_kses_post( html_entity_decode( $loop_end ) );

			if ( $paginate ) {
				$template_handler->get_template_part( 'pagination', '', '', array(
					'pages' => $pages
				) );
			} ?>

		<?php }

		wp_reset_postdata();

		return ob_get_clean();
	}

Code file location:

download-monitor/download-monitor/src/Shortcodes.php

Download Monitor [dlm_no_access] Shortcode

The Download Monitor shortcode is designed to handle access restrictions for downloads. It functions by checking if the ‘download-id’ or ‘action’ is set. If not, it returns an empty string. If set, it retrieves the download details and version. It also enables the addition of extra scripts related to the shortcode. The shortcode can display a ‘no access’ message based on the ‘show_message’ attribute. It then loads a ‘no access’ template with the download details and the ‘no access’ message.

Shortcode: [dlm_no_access]

Examples and Usage

Basic example – This shortcode displays a no-access page for a specified download, with the default message visibility set to true.

[dlm_no_access /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'dlm_no_access', array( $this, 'no_access_page' ) );

Shortcode PHP function:

function no_access_page( $atts ) {
		global $wp;

		// Action to allow the adition of extra scripts and code related to the shortcode
		do_action( 'dlm_dlm_no_access_shortcode_scripts' );

		// atts
		$atts = shortcode_atts( array(
			'show_message' => 'true',
		), $atts );

		// start buffer
		ob_start();

		// show_message must be a bool
		$atts['show_message'] = ( 'true' === $atts['show_message'] );

		// return empty string if download-id is not set or action is not no_access_dlm_xhr_download for XHR downloads & modal no access.
		if ( ( ! isset( $_REQUEST['action'] ) || 'no_access_dlm_xhr_download' !== $_REQUEST['action'] ) && ! isset( $wp->query_vars['download-id'] ) ) {
			return '';
		}

		$download_id = isset( $_REQUEST['download_id'] ) ? absint( $_REQUEST['download_id'] ) : absint( $wp->query_vars['download-id'] );

		// template handler.
		$template_handler = new DLM_Template_Handler();

		try {
			/** @var \DLM_Download $download */
			$download = download_monitor()->service( 'download_repository' )->retrieve_single( absint( $download_id ) );

			$version_id = '';

			if ( ! empty( $_GET['version'] ) ) {
				$version_id = $download->get_version_id_version_name( sanitize_text_field( wp_unslash( $_GET['version'] ) ) );
			}

			if ( ! empty( $_GET['v'] ) ) {
				$version_id = absint( $_GET['v'] );
			}

			if ( null != $download && $version_id ) {
				try {
					$version = download_monitor()->service( 'version_repository' )->retrieve_single( $version_id );
					$download->set_version( $version );
				} catch ( Exception $e ) {

				}
			}

			// load no access template
			$template_handler->get_template_part( 'no-access', '', '', array(
				'download'          => $download,
				'no_access_message' => ( ( $atts['show_message'] ) ? wp_kses_post( get_option( 'dlm_no_access_error', '' ) ) : '' )
			) );

		} catch ( Exception $exception ) {
			// no download with given ID
		}

		// set new content
		$content = ob_get_clean();

		return $content;
	}

Code file location:

download-monitor/download-monitor/src/Shortcodes.php

Conclusion

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