Simple Download Monitor Shortcodes

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

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

Plugin Icon
Simple Download Monitor

"Simple Download Monitor is a reliable WordPress plugin designed to manage, monitor, and keep track of your digital downloads with ease. It's user-friendly and efficient for all download needs."

★★★★✩ (113) Active Installs: 30000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [sdm_download]
  • [sdm_download_counter]
  • [sdm_latest_downloads]
  • [sdm_popular_downloads]
  • [sdm_download_link]
  • [sdm_show_all_dl]
  • [sdm_show_dl_from_category]
  • [sdm_download_categories]
  • [sdm_download_categories_list]
  • [sdm_search_form]
  • [sdm_show_download_info]

Simple Download Monitor [sdm_download] Shortcode

The Simple Download Monitor shortcode is used to create and manage download buttons for files. It gives you the ability to customize the download button’s appearance and behavior. This shortcode allows you to specify the ID of the downloadable file, the text on the download button, and whether the file should open in a new window. It also provides options for custom CSS classes and to show the file size or version. The shortcode ensures that the download link is password protected if necessary. It also integrates with reCAPTCHA and Terms & Conditions if enabled. The output can be styled using different fancy templates.

Shortcode: [sdm_download]

Parameters

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

  • id – Unique identifier of the downloadable item
  • fancy – Determines the display style of the download button
  • button_text – Customizes the text displayed on the download button
  • new_window – Controls whether the download opens in a new window
  • color – Customizes the color of the download button
  • css_class – Allows for the addition of custom CSS classes
  • show_size – Determines whether the size of the download is displayed
  • show_version – Determines whether the version of the download is displayed

Examples and Usage

Basic example – A simple usage of the shortcode to display a download button for the specified download ID.

[sdm_download id="123" /]

Advanced examples

Display a download button with custom button text and open the download in a new window.

[sdm_download id="123" button_text="Download Now" new_window="1" /]

Use the shortcode to display a download button with a custom color and additional CSS class. This can be useful for styling the button to match the rest of your site.

[sdm_download id="123" color="blue" css_class="my_custom_class" /]

Display a download button for a specified ID, with the download size and version number shown alongside the button.

[sdm_download id="123" show_size="1" show_version="1" /]

Use the shortcode to display a download button in a fancy style. The ‘fancy’ parameter can be set to 1, 2, or 3 to select different styles.

[sdm_download id="123" fancy="1" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'sdm_download', 'sdm_create_download_shortcode' );  // For download shortcode (underscores)

Shortcode PHP function:

function sdm_create_download_shortcode( $atts ) {

	$shortcode_atts = sanitize_sdm_create_download_shortcode_atts(
		shortcode_atts(
			array(
				'id'           => '',
				'fancy'        => '0',
				'button_text'  => sdm_get_default_download_button_text( $atts['id'] ),
				'new_window'   => '',
				'color'        => '',
				'css_class'    => '',
				'show_size'    => '',
				'show_version' => '',
			),
			$atts
		)
	);

	// Make shortcode attributes available in function local scope.
	extract( $shortcode_atts );

	if ( empty( $id ) ) {
		return '<p style="color: red;">' . __( 'Error! Please enter an ID value with this shortcode.', 'simple-download-monitor' ) . '</p>';
	}

		$id        = intval( $id );
		$color     = sdm_sanitize_text( $color );
		$css_class = sdm_sanitize_text( $css_class );

	// Check to see if the download link cpt is password protected
	$get_cpt_object  = get_post( $id );
	$cpt_is_password = ! empty( $get_cpt_object->post_password ) ? 'yes' : 'no';  // yes = download is password protected;
	// Get CPT title
	$item_title = get_the_title( $id );

	//*** Generate the download now button code ***
	if ( empty( $new_window ) ) {
		$new_window = get_post_meta( $id, 'sdm_item_new_window', true );
	}
	$window_target = empty( $new_window ) ? '_self' : '_blank';

	$homepage             = get_bloginfo( 'url' );
	$download_url         = $homepage . '/?sdm_process_download=1&download_id=' . $id;
	$download_button_code = '<a href="' . $download_url . '" class="sdm_download ' . esc_attr($color) . '" title="' . esc_html($item_title) . '" target="' . $window_target . '">' . esc_attr($button_text) . '</a>';

	$main_advanced_opts = get_option( 'sdm_advanced_options' );

	//Check if Terms & Condition enabled
	$termscond_enable = isset( $main_advanced_opts['termscond_enable'] ) ? true : false;
	if ( $termscond_enable ) {
		$download_button_code = sdm_get_download_form_with_termsncond( $id, $shortcode_atts, 'sdm_download ' . $color );
	}

	//Check if reCAPTCHA enabled
	$recaptcha_enable = isset( $main_advanced_opts['recaptcha_enable'] ) ? true : false;
	if ( $recaptcha_enable && $cpt_is_password == 'no' ) {
		$download_button_code = sdm_get_download_form_with_recaptcha( $id, $shortcode_atts, 'sdm_download ' . $color );
	}

	if ( $cpt_is_password !== 'no' ) {//This is a password protected download so replace the download now button with password requirement
		$download_button_code = sdm_get_password_entry_form( $id, $shortcode_atts, 'sdm_download ' . $color );
	}
	//End of download now button code generation

	$output = '';
	switch ( $fancy ) {
		case '1':
			include_once 'includes/templates/fancy1/sdm-fancy-1.php';
			$output .= sdm_generate_fancy1_display_output( $shortcode_atts );
			$output .= '<div class="sdm_clear_float"></div>';
			break;
		case '2':
			include_once 'includes/templates/fancy2/sdm-fancy-2.php';
                        wp_enqueue_style( 'sdm_addons_listing', WP_SIMPLE_DL_MONITOR_URL . '/includes/templates/fancy2/sdm-fancy-2-styles.css', array(), WP_SIMPLE_DL_MONITOR_VERSION );
			$output .= sdm_generate_fancy2_display_output( $shortcode_atts );
			$output .= '<div class="sdm_clear_float"></div>';
			break;
		case '3':
			include_once 'includes/templates/fancy3/sdm-fancy-3.php';
			$output .= sdm_generate_fancy3_display_output( $shortcode_atts );
			$output .= '<div class="sdm_clear_float"></div>';
			break;
		default: // Default output is the standard download now button (fancy 0)
			include_once 'includes/templates/fancy0/sdm-fancy-0.php';
			$output .= sdm_generate_fancy0_display_output( $shortcode_atts );
	}

	return apply_filters( 'sdm_download_shortcode_output', $output, $atts );
}

Code file location:

simple-download-monitor/simple-download-monitor/sdm-shortcodes.php

Simple Download Monitor [sdm_download_counter] Shortcode

The Simple Download Monitor shortcode is a tool that tracks and displays the total number of downloads for a specific item or all items. This shortcode extracts the ID value from the shortcode attributes. If no ID is found, it returns an error message. If the ID is ‘all’, it retrieves the total download count for all posts. Else, it fetches the download count for the specified post ID. The output is a div element containing the download count and a string indicating ‘Download’ or ‘Downloads’, depending on the count.

Shortcode: [sdm_download_counter]

Parameters

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

  • id – Unique identifier to specify a particular download or ‘all’ for total downloads.

Examples and Usage

Basic example – The shortcode below is a basic usage example. It uses the ‘id’ parameter to specify the download item whose count you want to display.

[sdm_download_counter id="123"]

Advanced examples

The shortcode below uses the ‘id’ parameter with a special value ‘all’. This will display the total download count for all download items.

[sdm_download_counter id="all"]

The shortcode below uses the ‘id’ parameter to specify the download item whose count you want to display. It also uses the ‘class’ parameter to add a custom CSS class to the download counter, allowing you to style it as per your needs.

[sdm_download_counter id="123" class="my_custom_class"]

Please note that the ‘class’ parameter is not defined in the provided PHP code for the shortcode. You would need to modify the PHP code to accept and handle this parameter if you want to use it.

PHP Function Code

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

Shortcode line:

add_shortcode( 'sdm_download_counter', 'sdm_create_counter_shortcode' );  // For counter shortcode (underscores)

Shortcode PHP function:

function sdm_create_counter_shortcode( $atts ) {

	extract(
		shortcode_atts(
			array(
				'id' => '',
			),
			$atts
		)
	);

	if ( empty( $id ) ) {
		return '<p style="color: red;">' . __( 'Error! Please enter an ID value with this shortcode.', 'simple-download-monitor' ) . '</p>';
	}

	// Checks if to show count for all total download or any specific download.
	if ( preg_match( '/^all$/i', $id ) ) {
		$db_count = sdm_get_download_count_for_all_posts();
	} else {
		$db_count = sdm_get_download_count_for_post( $id );
	}

	// Set string for singular/plural results
	$string = ( $db_count == '1' ) ? __( 'Download', 'simple-download-monitor' ) : __( 'Downloads', 'simple-download-monitor' );

	$output = '<div class="sdm_download_count"><span class="sdm_count_number">' . $db_count . '</span><span class="sdm_count_string"> ' . $string . '</span></div>';
	// Return result
	return apply_filters( 'sdm_download_count_output', $output, $atts );
}

Code file location:

simple-download-monitor/simple-download-monitor/sdm-shortcodes.php

Simple Download Monitor [sdm_latest_downloads] Shortcode

The Simple Download Monitor shortcode displays the latest downloads. It’s customizable, allowing you to set the number of downloads shown, the order, and category.

Shortcode: [sdm_latest_downloads]

Parameters

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

  • number – Specifies the number of latest downloads to show.
  • fancy – Determines the style of the download button, ranging from 0-3.
  • button_text – Allows customization of the download button text.
  • new_window – If set, the download opens in a new window.
  • orderby – Determines the order of downloads based on attributes like date.
  • order – Sets the sorting order, either ascending or descending.
  • category_slug – Filters downloads based on the specified category.

Examples and Usage

Basic Example – Displaying the five most recent downloads.

[sdm_latest_downloads number=5 /]

Advanced Examples

Displaying the five most recent downloads in a new window with a specific button text.

[sdm_latest_downloads number=5 new_window=1 button_text="Download Now" /]

Displaying the five most recent downloads from a specific category (let’s say the category slug is ‘ebooks’).

[sdm_latest_downloads number=5 category_slug='ebooks' /]

Displaying the five most recent downloads, ordered by title in ascending order.

[sdm_latest_downloads number=5 orderby='post_title' order='ASC' /]

Displaying the five most recent downloads with fancy style 1.

[sdm_latest_downloads number=5 fancy=1 /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'sdm_latest_downloads', 'sdm_show_latest_downloads' ); // For showing X number of latest downloads

Shortcode PHP function:

function sdm_show_latest_downloads( $args ) {

	extract(
		shortcode_atts(
			array(
				'number'        => '5',
				'fancy'         => '0',
				'button_text'   => '',
				'new_window'    => '',
				'orderby'       => 'post_date',
				'order'         => 'DESC',
				'category_slug' => '',
			),
			$args
		)
	);

	$query_args = array(
		'post_type'      => 'sdm_downloads',
		'show_posts'     => -1,
		'posts_per_page' => $number,
		'orderby'        => $orderby,
		'order'          => $order,
	);

	//Check if the query needs to be for a category
	if ( ! empty( $category_slug ) ) {
		$field = 'slug';
		$terms = $category_slug;

		//Add the category slug parameters for the query args
		$query_args['tax_query'] = array(
			array(
				'taxonomy' => 'sdm_categories',
				'field'    => $field,
				'terms'    => $terms,
			),
		);
	}

	// Query cpt's based on arguments above
	$get_posts = get_posts( $query_args );

	// If no cpt's are found
	if ( ! $get_posts ) {
		return '<p style="color: red;">' . __( 'There are no download items matching this shortcode criteria.', 'simple-download-monitor' ) . '</p>';
	}
	// Else iterate cpt's
	else {

		$output = '';
		if ( $fancy == '0' ) {
			include_once WP_SIMPLE_DL_MONITOR_PATH . 'includes/templates/fancy0/sdm-fancy-0.php';
			$output .= sdm_generate_fancy0_latest_downloads_display_output( $get_posts, $args );
		}
		if ( $fancy == '1' ) {
			include_once WP_SIMPLE_DL_MONITOR_PATH . 'includes/templates/fancy1/sdm-fancy-1.php';
			$output .= sdm_generate_fancy1_latest_downloads_display_output( $get_posts, $args );
		} elseif ( $fancy == '2' ) {
			include_once WP_SIMPLE_DL_MONITOR_PATH . 'includes/templates/fancy2/sdm-fancy-2.php';
			$output .= sdm_generate_fancy2_latest_downloads_display_output( $get_posts, $args );
		} elseif ( $fancy == '3' ) {
			include_once WP_SIMPLE_DL_MONITOR_PATH . 'includes/templates/fancy3/sdm-fancy-3.php';
			$output .= sdm_generate_fancy3_latest_downloads_display_output( $get_posts, $args );
		}

		// Return results
		return apply_filters( 'sdm_latest_downloads_shortcode_output', $output, $args, $get_posts );
	}  // End else iterate cpt's

}

Code file location:

simple-download-monitor/simple-download-monitor/sdm-shortcodes.php

Simple Download Monitor [sdm_popular_downloads] Shortcode

The Simple Download Monitor plugin shortcode, ‘sdm_popular_downloads’, displays a specified number of popular downloads. It has several parameters like ‘number’, ‘fancy’, ‘button_text’, ‘new_window’, ‘orderby’, ‘order’, ‘category_slug’. These help to customize the display of popular downloads. If ‘category_slug’ is provided, it fetches popular downloads from that category. If not, it fetches from all downloads. The ‘fancy’ parameter determines the display style.

Shortcode: [sdm_popular_downloads]

Parameters

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

  • number – Determines the number of popular downloads to display.
  • fancy – Defines the visual style of the download list.
  • button_text – Customizes the text on the download button.
  • new_window – Decides if download opens in a new window.
  • orderby – Specifies the attribute to sort downloads by.
  • order – Sets the order of downloads, ascending or descending.
  • category_slug – Filters downloads by a specific category.

Examples and Usage

Basic example – Utilizing the shortcode to display the top 5 most popular downloads in a simple format.

[sdm_popular_downloads number=5 fancy=0 /]

Advanced examples

Displaying the top 10 popular downloads, sorted by the download count, in a fancy format. The downloads will open in a new window.

[sdm_popular_downloads number=10 fancy=1 new_window=1 orderby='cnt' order='DESC' /]

Showing the top 3 popular downloads from a specific category, sorted by post date, in a fancy format. The category is identified by its slug.

[sdm_popular_downloads number=3 fancy=1 category_slug='ebooks' /]

Displaying the top 5 popular downloads with a custom button text, sorted by post date, in a simple format.

[sdm_popular_downloads number=5 fancy=0 button_text='Download Now' /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'sdm_popular_downloads', 'sdm_show_popular_downloads' ); // For showing X number of popular downloads

Shortcode PHP function:

function sdm_show_popular_downloads( $args ) {

	extract(
		shortcode_atts(
			array(
				'number'        => '5',
				'fancy'         => '0',
				'button_text'   => '',
				'new_window'    => '',
				'orderby'       => 'post_date',
				'order'         => 'DESC',
				'category_slug' => '',
			),
			$args
		)
	);

	global $wpdb;
	//Check if the query needs to be for a category
	if ( ! empty( $category_slug ) ) {
		$q = 'SELECT posts.*, downloads.id, downloads.post_id, terms.*, termrel.*, postmeta.*, (COUNT(downloads.post_id) + postmeta.meta_value) AS cnt'
		. ' FROM ' . $wpdb->prefix . 'posts as posts, ' . $wpdb->prefix . 'sdm_downloads as downloads, ' . $wpdb->prefix . 'terms as terms, ' . $wpdb->prefix . 'term_relationships as termrel, ' . $wpdb->prefix . 'postmeta as postmeta WHERE'
		. ' posts.id=downloads.post_id'
		. " AND (postmeta.meta_key='sdm_count_offset' AND postmeta.post_id=downloads.post_id)"
		. ' AND (terms.slug= %s AND termrel.object_id=downloads.post_id AND termrel.term_taxonomy_id=terms.term_id)'
		. ' GROUP BY downloads.post_id'
		. ' ORDER BY cnt DESC, %s %s'
		. ' LIMIT %d;';
		$q = $wpdb->prepare( $q, $category_slug, $orderby, $order, $number );
	} else {
		//no categury_slug present
		$q = 'SELECT posts.*, downloads.id, downloads.post_id, postmeta.*, (COUNT(downloads.post_id) + postmeta.meta_value) AS cnt'
		. ' FROM ' . $wpdb->prefix . 'posts as posts, ' . $wpdb->prefix . 'sdm_downloads as downloads, ' . $wpdb->prefix . 'postmeta as postmeta WHERE'
		. ' posts.id=downloads.post_id'
		. " AND (postmeta.meta_key='sdm_count_offset' AND postmeta.post_id=downloads.post_id)"
		. ' GROUP BY downloads.post_id'
		. ' ORDER BY cnt DESC, %s %s'
		. ' LIMIT %d;';
		$q = $wpdb->prepare( $q, $orderby, $order, $number );
	}

	$get_posts = $wpdb->get_results( $q );

	// If no cpt's are found
	if ( ! $get_posts ) {
		return '<p style="color: red;">' . __( 'There are no download items matching this shortcode criteria.', 'simple-download-monitor' ) . '</p>';
	}
	// Else iterate cpt's
	else {

		$output = '';
		if ( $fancy == '0' ) {
			include_once WP_SIMPLE_DL_MONITOR_PATH . 'includes/templates/fancy0/sdm-fancy-0.php';
			$output .= sdm_generate_fancy0_popular_downloads_display_output( $get_posts, $args );
		} elseif ( $fancy == '1' ) {
			include_once WP_SIMPLE_DL_MONITOR_PATH . 'includes/templates/fancy1/sdm-fancy-1.php';
			$output .= sdm_generate_fancy1_popular_downloads_display_output( $get_posts, $args );
		} elseif ( $fancy == '2' ) {
			include_once WP_SIMPLE_DL_MONITOR_PATH . 'includes/templates/fancy2/sdm-fancy-2.php';
			$output .= sdm_generate_fancy2_popular_downloads_display_output( $get_posts, $args );
		} elseif ( $fancy == '3' ) {
			include_once WP_SIMPLE_DL_MONITOR_PATH . 'includes/templates/fancy3/sdm-fancy-3.php';
			$output .= sdm_generate_fancy3_popular_downloads_display_output( $get_posts, $args );
		}

		// Return results
		return apply_filters( 'sdm_popular_downloads_shortcode_output', $output, $args, $get_posts );
	}  // End else iterate cpt's
}

Code file location:

simple-download-monitor/simple-download-monitor/sdm-shortcodes.php

Simple Download Monitor [sdm_download_link] Shortcode

The Simple Download Monitor shortcode is a useful tool that creates a download link. This shortcode takes an ‘id’ as an attribute, which is the ID of the download item. If no ‘id’ is provided, an error message is displayed. When used correctly, it generates a URL that triggers the download process when clicked.

Shortcode: [sdm_download_link]

Parameters

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

  • id – unique identifier of the download item

Examples and Usage

Basic example – A simple usage of the ‘sdm_download_link’ shortcode to generate a download link for a specific file by referencing its ID.

[sdm_download_link id=1 /]

Advanced example – In this scenario, we are using the ‘sdm_download_link’ shortcode to generate download links for multiple files. Each shortcode is referencing a different ID, which corresponds to a different file in the Simple Download Monitor plugin’s database.

[sdm_download_link id=1 /]
[sdm_download_link id=2 /]
[sdm_download_link id=3 /]

Note: In the above examples, replace the number after ‘id=’ with the ID of the file you want to create a download link for. The ID for each file can be found in the Simple Download Monitor plugin’s ‘Downloads’ page in your WordPress dashboard.

PHP Function Code

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

Shortcode line:

add_shortcode( 'sdm_download_link', 'sdm_create_simple_download_link' );

Shortcode PHP function:

function sdm_create_simple_download_link( $atts ) {
	extract(
		shortcode_atts(
			array(
				'id' => '',
			),
			$atts
		)
	);

	if ( empty( $id ) ) {
		return '<p style="color: red;">' . __( 'Error! Please enter an ID value with this shortcode.', 'simple-download-monitor' ) . '</p>';
	}

	return WP_SIMPLE_DL_MONITOR_SITE_HOME_URL . '/?sdm_process_download=1&download_id=' . $id;
}

Code file location:

simple-download-monitor/simple-download-monitor/sdm-shortcodes.php

Simple Download Monitor [sdm_show_all_dl] Shortcode

The Simple Download Monitor plugin shortcode, ‘sdm_show_all_dl’, is designed to display all downloads. It disregards any category filters, ensuring all downloads are visible. The ‘category_id’ and ‘category_slug’ arguments are unset, and the ‘show_all’ argument is set to 1.

Shortcode: [sdm_show_all_dl]

Parameters

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

  • category_id – Identifier to filter downloads by category.
  • category_slug – Alternative way to filter downloads using category slug.
  • show_all – Enables display of all downloads.

Examples and Usage

Basic example – Show all downloads without specifying any category.

[sdm_show_all_dl]

Advanced examples

Display all downloads but exclude those from a specific category by ID. The plugin will first try to load all downloads, and then exclude those from the specified category ID.

[sdm_show_all_dl category_id="2"]

Similar to the previous example, but this time we exclude the downloads from a specific category by using the category slug. This can be useful if you don’t know the ID of the category but you know its slug.

[sdm_show_all_dl category_slug="ebooks"]

PHP Function Code

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

Shortcode line:

add_shortcode( 'sdm_show_all_dl', 'sdm_handle_show_all_dl_shortcode' ); // For show all downloads shortcode

Shortcode PHP function:

function sdm_handle_show_all_dl_shortcode( $args ) {
	if ( isset( $args['category_id'] ) ) {
		unset( $args['category_id'] );
	}
	if ( isset( $args['category_slug'] ) ) {
		unset( $args['category_slug'] );
	}
	$args['show_all'] = 1;
	return sdm_handle_category_shortcode( $args );
}

Code file location:

simple-download-monitor/simple-download-monitor/sdm-shortcodes.php

Simple Download Monitor [sdm_show_dl_from_category] Shortcode

The Simple Download Monitor shortcode allows users to display and manage download items based on category criteria. It fetches download items from a specified category, either by slug or ID. It also enables customization of the download button, including text and color. If both category slug and ID are empty, or if both are defined, an error message is displayed. The shortcode also supports pagination and allows users to set the number of posts per page. In case no download items match the category criteria, an error message is displayed. If the terms and conditions or reCAPTCHA are enabled, it integrates them into the download process. Finally, it supports multiple fancy display templates and applies filters to the shortcode output, allowing further customization.

Shortcode: [sdm_show_dl_from_category]

Parameters

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

  • category_slug – Slug of the category to show downloads from.
  • category_id – ID of the category to show downloads from.
  • fancy – Determines the style of the download display (0, 1, 2, or 3).
  • button_text – Customizes the text of the download button.
  • new_window – Determines if download opens in a new window.
  • orderby – Specifies the order of the downloads (post_date, title, etc).
  • order – Determines the direction of the order (ASC or DESC).
  • pagination – Sets the number of downloads per page.
  • show_all – If set, shows all downloads regardless of category.

Examples and Usage

Basic Example – Display downloads from a specific category by using the category slug

[sdm_show_dl_from_category category_slug="ebooks" /]

Advanced Examples

Display downloads from a specific category by using the category ID, with a custom download button text, and the downloads ordered by title in ascending order.

[sdm_show_dl_from_category category_id=3 button_text="Get Your Download" orderby="title" order="ASC" /]

Display downloads from multiple categories by using the category IDs, with pagination set to display 5 downloads per page.

[sdm_show_dl_from_category category_id="3,4,5" pagination="5" /]

Display downloads from a specific category by using the category slug, with the download link opening in a new window.

[sdm_show_dl_from_category category_slug="ebooks" new_window="1" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'sdm_show_dl_from_category', 'sdm_handle_category_shortcode' ); //For category shortcode

Shortcode PHP function:

function sdm_handle_category_shortcode( $args ) {

	extract(
		shortcode_atts(
			array(
				'category_slug' => '',
				'category_id'   => '',
				'fancy'         => '0',
				'button_text'   => __( 'Download Now!', 'simple-download-monitor' ),
				'new_window'    => '',
				'orderby'       => 'post_date',
				'order'         => 'DESC',
				'pagination'    => '',
			),
			$args
		)
	);

	// Define vars
	$field = '';
	$terms = '';

	// If category slug and category id are empty.. return error
	if ( empty( $category_slug ) && empty( $category_id ) && empty( $args['show_all'] ) ) {
		return '<p style="color: red;">' . __( 'Error! You must enter a category slug OR a category id with this shortcode. Refer to the documentation for usage instructions.', 'simple-download-monitor' ) . '</p>';
	}

	// If both category slug AND category id are defined... return error
	if ( ! empty( $category_slug ) && ! empty( $category_id ) ) {
		return '<p style="color: red;">' . __( 'Error! Please enter a category slug OR id; not both.', 'simple-download-monitor' ) . '</p>';
	}

	// Else setup query arguments for category_slug
	if ( ! empty( $category_slug ) && empty( $category_id ) ) {

		$field = 'slug';

		$terms = array_filter(
			explode( ',', $category_slug ),
			function( $value ) {
				return ! empty( $value ) ? trim( $value ) : false;
			}
		);
	}
	// Else setup query arguments for category_id
	elseif ( ! empty( $category_id ) && empty( $category_slug ) ) {

		$field = 'term_id';
		//$terms = $category_id;
		$terms = array_filter(
			explode( ',', $category_id ),
			function( $value ) {
				return ! empty( $value ) ? trim( $value ) : false;
			}
		);
	}

	if ( isset( $args['show_all'] ) ) {
		$tax_query = array();
	} else {
		$tax_query = array(
			array(
				'taxonomy' => 'sdm_categories',
				'field'    => $field,
				'terms'    => $terms,
			),
		);
	}

	// For pagination
	$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
	if ( isset( $args['pagination'] ) ) {
		if ( ! is_numeric( $args['pagination'] ) ) {
			return '<p style="color: red;">' . __( 'Error! You must enter a numeric number for the "pagination" parameter of the shortcode. Refer to the usage documentation.', 'simple-download-monitor' ) . '</p>';
		}
		$posts_per_page = $args['pagination'];
	} else {
		$posts_per_page = 9999;
	}

	// Query cpt's based on arguments above
	$get_posts_args = array(
		'post_type'      => 'sdm_downloads',
		'show_posts'     => -1,
		'posts_per_page' => $posts_per_page,
		'tax_query'      => $tax_query,
		'orderby'        => $orderby,
		'order'          => $order,
		'paged'          => $paged,
	);

	$query = new WP_Query();

	$get_posts = $query->query( $get_posts_args );

	// If no cpt's are found
	if ( ! $get_posts ) {
		return '<p style="color: red;">' . __( 'There are no download items matching this category criteria.', 'simple-download-monitor' ) . '</p>';
	}
	// Else iterate cpt's
	else {

		$output = '';

		// See if user color option is selected
		$main_opts = get_option( 'sdm_downloads_options' );
		$color_opt = isset( $main_opts['download_button_color'] ) ? $main_opts['download_button_color'] : null;
		$def_color = isset( $color_opt ) ? str_replace( ' ', '', strtolower( $color_opt ) ) : 'green';

		if ( $fancy == '0' ) {

			// Setup download location
			$homepage = get_bloginfo( 'url' );
			if ( empty( $new_window ) ) {
				$new_window = get_post_meta( $id, 'sdm_item_new_window', true );
			}

			$window_target = empty( $new_window ) ? '_self' : '_blank';

			// Iterate cpt's
			foreach ( $get_posts as $item ) {

				// Set download location
				$id           = $item->ID;  // get each cpt ID
				$download_url = $homepage . '/?sdm_process_download=1&download_id=' . $id;

				// Get each cpt title
				$item_title = get_the_title( $id );

				// Setup download button code
				$download_button_code = '<a href="' . $download_url . '" class="sdm_download ' . $def_color . '" title="' . esc_html($item_title) . '" target="' . $window_target . '">' . esc_attr($button_text) . '</a>';

				$main_advanced_opts = get_option( 'sdm_advanced_options' );

				//Check if Terms & Condition enabled
				$termscond_enable = isset( $main_advanced_opts['termscond_enable'] ) ? true : false;
				if ( $termscond_enable ) {
					$download_button_code = sdm_get_download_form_with_termsncond( $id, $args, 'sdm_download ' . $def_color );
				}

				//Check if reCAPTCHA enabled
				$recaptcha_enable = isset( $main_advanced_opts['recaptcha_enable'] ) ? true : false;
				if ( $recaptcha_enable ) {
					$download_button_code = sdm_get_download_form_with_recaptcha( $id, $args, 'sdm_download ' . $def_color );
				}

				// Generate download buttons
				$output .= '<div class="sdm_download_link">' . $download_button_code . '</div><br />';
			}  // End foreach
		}
		// Fancy 1 and onwards handles the loop inside the template function
		elseif ( $fancy == '1' ) {
			include_once 'includes/templates/fancy1/sdm-fancy-1.php';
			$output .= sdm_generate_fancy1_category_display_output( $get_posts, $args );
		} elseif ( $fancy == '2' ) {
			include_once 'includes/templates/fancy2/sdm-fancy-2.php';
			$output .= sdm_generate_fancy2_category_display_output( $get_posts, $args );
		} elseif ( $fancy == '3' ) {
			include_once 'includes/templates/fancy3/sdm-fancy-3.php';
			$output .= sdm_generate_fancy3_category_display_output( $get_posts, $args );
		}

		// Pagination related
		if ( isset( $args['pagination'] ) ) {
			$posts_per_page      = $args['pagination'];
			$count_sdm_posts     = $query->found_posts;
			$published_sdm_posts = $count_sdm_posts;
			$total_pages         = ceil( $published_sdm_posts / $posts_per_page );

			$big        = 999999999; // Need an unlikely integer
			$pagination = paginate_links(
				array(
					'base'      => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
					'format'    => '',
					'add_args'  => '',
					'current'   => max( 1, get_query_var( 'paged' ) ),
					'total'     => $total_pages,
					'prev_text' => '&laquo;',
					'next_text' => '&raquo;',
				)
			);
			$output    .= '<div class="sdm_pagination">' . $pagination . '</div>';
		}

		// Return results
		return apply_filters( 'sdm_category_download_items_shortcode_output', $output, $args, $get_posts );
	}  // End else iterate cpt's
}

Code file location:

simple-download-monitor/simple-download-monitor/sdm-shortcodes.php

Simple Download Monitor [sdm_download_categories] Shortcode

The Simple Download Monitor shortcode is a functional tool that enables the display of download categories. It uses AJAX to create a file tree browser, allowing for interactive navigation. This shortcode fetches terms from the ‘sdm_categories’ taxonomy and displays them in a list format. It uses recursion to display child elements, if any. The output is enclosed in a ‘div’ tag with the class ‘sdm_object_tree’, providing a structured view for downloads.

Shortcode: [sdm_download_categories]

Examples and Usage

Basic example – The following shortcode example displays a list of download categories from the Simple Download Monitor plugin.

[sdm_download_categories]

PHP Function Code

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

Shortcode line:

add_shortcode( 'sdm_download_categories', 'sdm_download_categories_shortcode' ); // Ajax file tree browser

Shortcode PHP function:

function sdm_download_categories_shortcode() {

	function custom_taxonomy_walker( $taxonomy, $parent = 0 ) {

		// Get terms (check if has parent)
		$terms = get_terms(
			$taxonomy,
			array(
				'parent'     => $parent,
				'hide_empty' => false,
			)
		);

		// If there are terms, start displaying
		if ( count( $terms ) > 0 ) {
			// Displaying as a list
			$out = '<ul>';
			// Cycle though the terms
			foreach ( $terms as $term ) {
				// Secret sauce. Function calls itself to display child elements, if any
				$out .= '<li class="sdm_cat" id="' . $term->slug . '"><span id="' . $term->term_id . '" class="sdm_cat_title" style="cursor:pointer;">' . $term->name . '</span>';
				$out .= '<p class="sdm_placeholder" style="margin-bottom:0;"></p>' . custom_taxonomy_walker( $taxonomy, $term->term_id );
				$out .= '</li>';
			}
			$out .= '</ul>';
			return $out;
		}
		return;
	}

	return '<div class="sdm_object_tree">' . custom_taxonomy_walker( 'sdm_categories' ) . '</div>';
}

Code file location:

simple-download-monitor/simple-download-monitor/sdm-shortcodes.php

Simple Download Monitor [sdm_download_categories_list] Shortcode

The Simple Download Monitor plugin shortcode, ‘sdm_download_categories_list’, lists download categories. It allows customization of the appearance and content of the list. The shortcode enables the display of empty categories, numbered lists, item count, and hierarchical subcategories. It wraps the list in a class for styling.

Shortcode: [sdm_download_categories_list]

Parameters

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

  • class – Specifies the wrapper class of the download categories.
  • empty – Controls the visibility of empty categories. ‘0’ hides, ‘1’ shows.
  • numbered – Determines list type. ‘0’ for unordered, ‘1’ for ordered list.
  • count – Controls display of item counts. ‘0’ hides, ‘1’ shows item counts.
  • hierarchical – Controls subcategories display. ‘0’ hides, ‘1’ shows subcategories.

Examples and Usage

Basic example – A simple usage of the sdm_download_categories_list shortcode to display a list of download categories.

[sdm_download_categories_list]

Advanced examples

Displaying a list of download categories with a custom class, showing empty categories, using an ordered list, displaying the count of items in every category and displaying subcategories as well.

[sdm_download_categories_list class="custom-class" empty="1" numbered="1" count="1" hierarchical="1"]

Using the shortcode to display a list of download categories without showing empty categories, using an unordered list, and not displaying the count of items in every category. This example also shows the use of a custom class for the list.

[sdm_download_categories_list class="another-custom-class" empty="0" numbered="0" count="0"]

Displaying a list of download categories without showing subcategories. This example uses the default class for the list.

[sdm_download_categories_list hierarchical="0"]

PHP Function Code

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

Shortcode line:

add_shortcode( 'sdm_download_categories_list', 'sdm_download_categories_list_shortcode' );

Shortcode PHP function:

function sdm_download_categories_list_shortcode( $attributes ) {

	$atts = shortcode_atts(
		array(
			'class'        => 'sdm-download-categories', // wrapper class
			'empty'        => '0', // show empty categories
			'numbered'     => '0', // use <ol> instead of <ul> to wrap the list
			'count'        => '0', // display count of items in every category
			'hierarchical' => '1', // display subcategories as well
		),
		$attributes
	);

	return '<div class="' . esc_attr( $atts['class'] ) . '">'
	. sdm_download_categories_list_walker( $atts )
	. '</div>';
}

Code file location:

simple-download-monitor/simple-download-monitor/sdm-shortcodes.php

Simple Download Monitor [sdm_search_form] Shortcode

The Simple Download Monitor shortcode creates a custom search form for downloads. It accepts various parameters like ‘fancy’, ‘class’, ‘placeholder’, and ‘description_max_length’. The shortcode searches for posted search terms in the title and description of published downloads. It ignores keywords less than 3 characters long and removes duplicate results.

Shortcode: [sdm_search_form]

Parameters

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

  • fancy – allows customization of the search form’s appearance
  • class – specifies the CSS class for styling the search form
  • placeholder – sets the placeholder text in the search input field
  • description_max_length – limits the length of the search description

Examples and Usage

Basic example – the shortcode displays a search form for the Simple Download Monitor plugin.

[sdm_search_form /]

Advanced examples

Using the shortcode to display a search form with a custom CSS class. This can be useful for applying custom styles to the search form.

[sdm_search_form class="my_custom_class" /]

Using the shortcode to display a search form with a custom placeholder text. This can be useful for providing a more specific prompt to your users.

[sdm_search_form placeholder="Search for downloads..." /]

Using the shortcode to display a search form with a custom description length. This can be useful for controlling the amount of text that appears in the search results.

[sdm_search_form description_max_length="100" /]

Using the shortcode to display a fancy search form with a custom CSS class, placeholder text, and description length. This demonstrates how you can combine multiple parameters to customize the search form to your needs.

[sdm_search_form fancy="1" class="my_custom_class" placeholder="Search for downloads..." description_max_length="100" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'sdm_search_form', 'sdm_search_form_shortcode' );

Shortcode PHP function:

function sdm_search_form_shortcode( $args ) {
	$atts = shortcode_atts(
		array(
			'fancy'                  => '',
			'class'                  => '', // wrapper class
			'placeholder'            => 'Search...', // placeholder for search input
			'description_max_length' => 50, // short description symbols count
		),
		$args
	);

	global $wpdb;

	// Check if we have a search value posted
	$s_term = isset( $_POST['sdm_search_term'] ) ? stripslashes( sanitize_text_field( esc_html( $_POST['sdm_search_term'] ) ) ) : '';

	if ( ! empty( $s_term ) ) {
		// we got search term posted
		$keywords_searched = array();
		$posts_collection  = array();
		$parts             = explode( ' ', $s_term );
		foreach ( $parts as $keyword ) {
			if ( strlen( $keyword ) < 3 ) {
				//Ignore keywords less than 3 characters long
				continue;
			}

			$keywords_searched[] = $keyword;

			$querystr     = "
	    SELECT $wpdb->posts.*, $wpdb->postmeta.meta_value as description
	    FROM $wpdb->posts, $wpdb->postmeta
	    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
	    AND $wpdb->posts.post_status = 'publish'
	    AND $wpdb->posts.post_type = 'sdm_downloads'
	    AND ($wpdb->posts.post_title LIKE '%$keyword%'
	    OR ($wpdb->postmeta.meta_key='sdm_description' AND $wpdb->postmeta.meta_value LIKE '%$keyword%') )
	    GROUP BY $wpdb->posts.ID
	    ";
			$query_result = $wpdb->get_results( $querystr, OBJECT );

			//Merge the results of this keyword with the collection array. Also remove any duplicate.
			$posts_collection = array_merge( $posts_collection, $query_result );
			//$posts_collection = array_unique(array_merge($posts_collection, $query_result), SORT_REGULAR);
		}

		//Lets get rid of any duplicates
		$unique_posts_collection_id = array();
		foreach ( $posts_collection as $key => $post ) {
			if ( in_array( $post->ID, $unique_posts_collection_id ) ) {
				//This is a duplicate.
				unset( $posts_collection[ $key ] );//Delete this entry from the collection.
			} else {
				//This post ID is not in the collection yet. Add it.
				$unique_posts_collection_id[] = $post->ID;
			}
		}

		//Create the result entries output using a template.
		$s_results = sdm_generate_search_result_using_template( $posts_collection, $atts );

		//Show the search result
		if ( ! empty( $s_results ) ) {
			$result_output  = '<h2 class="sdm_search_result_heading">' . __( 'Showing search results for ', 'simple-download-monitor' ) . '"' . $s_term . '"</h2>';
			$result_output .= '<div class="sdm_search_result_number_of_items">' . __( 'Number of items found: ', 'simple-download-monitor' ) . count( $posts_collection ) . '</div>';
			$result_output .= '<div class="sdm_search_result_keywords">' . __( 'Keywords searched: ', 'simple-download-monitor' ) . implode( ', ', $keywords_searched ) . '</div>';
			$result_output .= '<div class="sdm_search_result_listing">' . $s_results . '</div>';
		} else {
			$result_output = '<h2 class="sdm_search_result_heading">' . __( 'Nothing found for ', 'simple-download-monitor' ) . '"' . $s_term . '".</h2>';
		}
	}
	$out  = '';
	$out .= '<form id="sdm_search_form" class="' . sanitize_html_class( $atts['class'], '' ) . '" method="POST">';
	$out .= '<input type="search" class="search-field" name="sdm_search_term" value="' . $s_term . '" placeholder="' . sdm_sanitize_text( $atts['placeholder'] ) . '">';
	$out .= '<input type="submit" class="sdm_search_submit" name="sdm_search_submit" value="Search">';
	$out .= '</form>';
	$out .= isset( $result_output ) ? $result_output : '';

	return $out;
}

Code file location:

simple-download-monitor/simple-download-monitor/sdm-shortcodes.php

Simple Download Monitor [sdm_show_download_info] Shortcode

The Simple Download Monitor shortcode displays download information based on the specified parameters. It requires both ‘id’ and ‘download_info’ parameters to function. The shortcode returns specific download details like title, description, download URL, thumbnail, file size, file version, and download count. It’s a useful tool for managing and displaying download data on your WordPress site.

Shortcode: [sdm_show_download_info]

Parameters

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

  • id – Unique identifier of the download item.
  • download_info – Specifies the type of download information to display.
  • title – Displays the title of the download item.
  • description – Shows the description of the download item.
  • download_url – Provides the download URL of the item.
  • thumbnail – Displays the thumbnail of the download item.
  • thumbnail_url – Gives the raw URL of the download item’s thumbnail.
  • file_size – Shows the file size of the download item.
  • file_version – Displays the version of the download file.
  • download_count – Shows the number of times the download item has been downloaded.

Examples and Usage

Basic example – Displaying a download item’s title using its ID

[sdm_show_download_info id="123" download_info="title"]

Advanced examples

Displaying a download item’s description using its ID

[sdm_show_download_info id="123" download_info="description"]

Displaying a download item’s URL using its ID

[sdm_show_download_info id="123" download_info="download_url"]

Displaying a download item’s thumbnail using its ID

[sdm_show_download_info id="123" download_info="thumbnail"]

Displaying a download item’s file size using its ID

[sdm_show_download_info id="123" download_info="file_size"]

Displaying a download item’s file version using its ID

[sdm_show_download_info id="123" download_info="file_version"]

Displaying a download item’s download count using its ID

[sdm_show_download_info id="123" download_info="download_count"]

PHP Function Code

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

Shortcode line:

add_shortcode( 'sdm_show_download_info', 'sdm_show_download_info_shortcode' );

Shortcode PHP function:

function sdm_show_download_info_shortcode( $args ) {
	extract(
		shortcode_atts(
			array(
				'id'            => '',
				'download_info' => '',
			),
			$args
		)
	);

	if ( empty( $id ) || empty( $download_info ) ) {
		return '<div class="sdm_shortcode_error">Error! you must enter a value for "id" and "download_info" parameters.</div>';
	}

	//Available values: title, description, download_url, thumbnail, file_size, file_version, download_count

	$id             = absint( $id );
	$get_cpt_object = get_post( $id );

	if ( $download_info == 'title' ) {//download title
		$item_title = get_the_title( $id );
		return $item_title;
	}

	if ( $download_info == 'description' ) {//download description
		$item_description = sdm_get_item_description_output( $id );
		return $item_description;
	}

	if ( $download_info == 'download_url' ) {//download URL
		$download_link = get_post_meta( $id, 'sdm_upload', true );
		return $download_link;
	}

	if ( $download_info == 'thumbnail' ) {//download thumb
		$download_thumbnail = get_post_meta( $id, 'sdm_upload_thumbnail', true );
		$download_thumbnail = '<img class="sdm_download_thumbnail_image" src="' . $download_thumbnail . '" />';
		return $download_thumbnail;
	}

	if ( $download_info == 'thumbnail_url' ) {//download thumbnail raw URL
		$download_thumbnail = get_post_meta( $id, 'sdm_upload_thumbnail', true );
		return $download_thumbnail;
	}

	if ( $download_info == 'file_size' ) {//download file size
		$file_size = get_post_meta( $id, 'sdm_item_file_size', true );
		return $file_size;
	}

	if ( $download_info == 'file_version' ) {//download file version
		$file_version = get_post_meta( $id, 'sdm_item_version', true );
		return $file_version;
	}

	if ( $download_info == 'download_count' ) {//download count
		$dl_count = sdm_get_download_count_for_post( $id );
		return $dl_count;
	}

	return '<div class="sdm_shortcode_error">Error! The value of "download_info" field does not match any availalbe parameters.</div>';
}

Code file location:

simple-download-monitor/simple-download-monitor/sdm-shortcodes.php

Conclusion

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