DK PDF Shortcodes

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

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

Plugin Icon
DK PDF

"DK PDF is a dynamic WordPress plugin designed to convert posts, pages, and custom post types into downloadable PDFs, ensuring easy content sharing and accessibility."

★★★★✩ (41) Active Installs: 5000+ Tested with: 4.9.24 PHP Version: 5.6
Included Shortcodes:
  • [dkpdf-button]
  • [dkpdf-remove]
  • [dkpdf-pagebreak]
  • [dkpdf-columns]
  • [dkpdf-columnbreak]

DK PDF [dkpdf-button] Shortcode

The DK-PDF Button shortcode is a powerful tool that enables users to generate PDFs from posts or pages. It triggers the ‘dkpdf_button_shortcode’ function, which uses a template loader to fetch the ‘dkpdf-button’ template part. The output is then captured and returned, creating a PDF button.

Shortcode: [dkpdf-button]

Examples and Usage

Basic example – The following example demonstrates the simplest use of the dkpdf-button shortcode. It does not require any additional attributes or parameters.

[dkpdf-button /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'dkpdf-button', 'dkpdf_button_shortcode' );

Shortcode PHP function:

function dkpdf_button_shortcode( $atts, $content = null ) {

	$template = new DKPDF_Template_Loader;

	ob_start();

	$template->get_template_part( 'dkpdf-button' );

	return ob_get_clean();

}

Code file location:

dk-pdf/dk-pdf/includes/dkpdf-shortcodes.php

DK PDF [dkpdf-remove] Shortcode

The DK PDF plugin shortcode, ‘dkpdf-remove’, allows users to remove specific elements from the PDF version of a post. This shortcode takes a ‘tag’ attribute to specify which elements to remove. If no ‘tag’ is provided, the PDF will be generated without any changes. If a ‘tag’ is specified and the PDF query variable is set, the specified shortcode will be removed.

Shortcode: [dkpdf-remove]

Parameters

Here is a list of all possible dkpdf-remove shortcode parameters and attributes:

  • tag – Specifies the shortcode to be removed when generating a PDF.

Examples and Usage

Basic example – An example of a basic usage of the dkpdf-remove shortcode would be to remove a specific shortcode tag from the PDF version of a page. In this case, we are removing a shortcode tag named ‘gallery’.

[dkpdf-remove tag="gallery"]

Advanced examples

For more complex situations, you might want to remove multiple shortcode tags from the PDF version of a page. You can achieve this by using the dkpdf-remove shortcode multiple times. In the following example, we are removing two shortcode tags named ‘gallery’ and ‘contact-form’.

[dkpdf-remove tag="gallery"]
[dkpdf-remove tag="contact-form"]

Another advanced usage of the dkpdf-remove shortcode is when you want to conditionally remove a shortcode tag based on whether the page is being viewed as a PDF or not. In this scenario, you can wrap the dkpdf-remove shortcode around the shortcode you want to remove. If the page is being viewed as a PDF, the wrapped shortcode will be removed. If the page is not being viewed as a PDF, the wrapped shortcode will be displayed. In the following example, we are conditionally removing a shortcode tag named ‘contact-form’.

[dkpdf-remove tag="contact-form"]
[contact-form id="123"]
[/dkpdf-remove]

PHP Function Code

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

Shortcode line:

add_shortcode( 'dkpdf-remove', 'dkpdf_remove_shortcode' );

Shortcode PHP function:

function dkpdf_remove_shortcode( $atts, $content = null ) {
		$atts = shortcode_atts( array(
			'tag' => ''
		), $atts );
		$pdf = get_query_var( 'pdf' );
		$tag = sanitize_text_field( $atts['tag'] );
		if( $tag !== '' && $pdf )  {
				remove_shortcode( $tag );
				add_shortcode( $tag, '__return_false' );
				return do_shortcode( $content );
		} else if( $pdf ) {
				return '';
		}

		return do_shortcode( $content );
}

Code file location:

dk-pdf/dk-pdf/includes/dkpdf-shortcodes.php

DK PDF [dkpdf-pagebreak] Shortcode

The DK PDF Pagebreak shortcode is a tool for managing page breaks in PDF documents. When applied, this shortcode checks if the PDF query variable is set or if a PDF generation action has been triggered. If either condition is met, it inserts a page break. Otherwise, it returns an empty string, ensuring smooth document flow.

Shortcode: [dkpdf-pagebreak]

Examples and Usage

Basic example – The dkpdf-pagebreak shortcode is used to insert a page break in the PDF version of a WordPress post or page. This shortcode doesn’t have any parameters or attributes, it simply inserts a page break where it’s placed.

[dkpdf-pagebreak /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'dkpdf-pagebreak', 'dkpdf_pagebreak_shortcode' );

Shortcode PHP function:

function dkpdf_pagebreak_shortcode( $atts, $content = null ) {

	$pdf = get_query_var( 'pdf' );

  	if( apply_filters( 'dkpdf_hide_button_isset', isset( $_POST['dkpdfg_action_create'] ) ) ) {
    	if ( $pdf || apply_filters( 'dkpdf_hide_button_equal', $_POST['dkpdfg_action_create'] == 'dkpdfg_action_create' )  ) {

			$output = '<pagebreak />';

		} else {

			$output = '';

		}

	} else {

		if( $pdf ) {

			$output = '<pagebreak />';

		} else {

			$output = '';

		}

	}

	return $output;

}

Code file location:

dk-pdf/dk-pdf/includes/dkpdf-shortcodes.php

DK PDF [dkpdf-columns] Shortcode

The DK PDF Columns shortcode allows users to customize the layout of their PDFs. It controls the number of columns, column equality, and gap between them. . The code checks if a PDF is being generated. If so, it sanitizes input and applies the column settings. If not, it disables the column-break function.

Shortcode: [dkpdf-columns]

Parameters

Here is a list of all possible dkpdf-columns shortcode parameters and attributes:

  • columns – defines the number of columns in the PDF.
  • equal-columns – if set to ‘true’, all columns will have equal width.
  • gap – determines the space between each column in pixels.

Examples and Usage

Basic Example – The shortcode below provides a simple way to divide your content into two columns with a gap of 10 units between them. The ‘equal-columns’ attribute is set to ‘false’, meaning the columns will not be of equal width.

[dkpdf-columns columns=2 equal-columns=false gap=10]

Advanced Examples

The following shortcode example divides the content into three columns with equal width. The gap between the columns is set to 15 units. This is a more advanced usage of the shortcode that allows for more customization of the layout.

[dkpdf-columns columns=3 equal-columns=true gap=15]

In this next example, the shortcode divides the content into four columns without equal width. The gap between the columns is set to 20 units. This is another advanced usage of the shortcode that provides further control over the layout and appearance of the content.

[dkpdf-columns columns=4 equal-columns=false gap=20]

PHP Function Code

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

Shortcode line:

add_shortcode( 'dkpdf-columns', 'dkpdf_columns_shortcode' );

Shortcode PHP function:

function dkpdf_columns_shortcode( $atts, $content = null ) {

	$atts = shortcode_atts( array(
		'columns' => '2',
		'equal-columns' => 'false',
		'gap' => '10'
	), $atts );

	$pdf = get_query_var( 'pdf' );

	if( $pdf ) {
		$columns = sanitize_text_field( $atts['columns'] );
		$equal_columns = sanitize_text_field( $atts['equal-columns'] );
		$vAlign = $equal_columns == 'true' ? 'vAlign="justify"' : '';
		$gap = sanitize_text_field( $atts['gap'] );
		return '<columns column-count="'.$columns.'" '.$vAlign.' column-gap="'.$gap.'" />'.do_shortcode( $content ).'<columns column-count="1">';
	} else {
		remove_shortcode( 'dkpdf-columnbreak' );
		add_shortcode( 'dkpdf-columnbreak', '__return_false' );
		return do_shortcode( $content );
	}

}

Code file location:

dk-pdf/dk-pdf/includes/dkpdf-shortcodes.php

DK PDF [dkpdf-columnbreak] Shortcode

The DK PDF Column Break shortcode is a function designed to create a break in the content columns of a PDF document. This is particularly useful for managing the layout of your PDFs. When implemented, this shortcode checks if the PDF query variable is set. If it is, it inserts a column break, effectively splitting the content into separate columns.

Shortcode: [dkpdf-columnbreak]

Examples and Usage

Basic example – Utilize the dkpdf-columnbreak shortcode to insert a column break in your PDF.

[dkpdf-columnbreak /]

Advanced examples

Although the dkpdf-columnbreak shortcode does not accept parameters, it can be combined with other shortcodes for more complex use cases.

Example 1: Use the dkpdf-columnbreak shortcode with a text shortcode to create a column break after a specific paragraph in your PDF.

Your paragraph here.
[dkpdf-columnbreak /]

Example 2: Combine the dkpdf-columnbreak shortcode with a table shortcode to create a column break after a table in your PDF.

[table id=1 /]
[dkpdf-columnbreak /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'dkpdf-columnbreak', 'dkpdf_columnbreak_shortcode' );

Shortcode PHP function:

function dkpdf_columnbreak_shortcode( $atts, $content = null ) {
	$pdf = get_query_var( 'pdf' );
	if( $pdf ) {
		return '<columnbreak />';
	}
}

Code file location:

dk-pdf/dk-pdf/includes/dkpdf-shortcodes.php

Conclusion

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