PDF Embedder Shortcode

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

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

Plugin Icon
PDF Embedder

"PDF Embedder is a WordPress plugin that seamlessly incorporates PDF files into your site. With its simple slug, pdf-embedder, it's an accessible tool for all users to enhance their content."

★★★★✩ (167) Active Installs: 300000+ Tested with: 6.2.0 PHP Version: false
Included Shortcodes:
  • [pdf-embedder]

PDF Embedder [pdf-embedder] Shortcode

The ‘pdf-embedder’ shortcode is a tool for embedding PDF files into WordPress posts or pages. It requires a ‘url’ attribute pointing to the PDF file. Additional attributes include ‘width’, ‘height’, ‘toolbar’, ‘toolbarfixed’, and ‘title’. These attributes allow customization of the PDF viewer’s size, toolbar position, fixed toolbar option, and title respectively. The shortcode generates an HTML anchor tag with the class ‘pdfemb-viewer’, embedding the PDF into the webpage.

Shortcode: [pdf-embedder]

Parameters

Here is a list of all possible pdf-embedder shortcode parameters and attributes:

  • url – link to the PDF file you want to embed
  • width – sets the width of the PDF viewer (in pixels, ‘max’, or ‘auto’)
  • height – sets the height of the PDF viewer (in pixels, ‘max’, or ‘auto’)
  • toolbar – determines the toolbar position (‘top’, ‘bottom’, ‘both’, ‘none’)
  • toolbarfixed – sets whether the toolbar is fixed (‘on’, ‘off’)
  • title – creates a custom title for the PDF file

Examples and Usage

Basic example – A simple usage of the PDF Embedder shortcode to display a PDF file on your WordPress site. It only requires the URL of the PDF file to be embedded.

[pdf-embedder url="http://example.com/path-to-your-pdf.pdf" /]

Advanced examples

Embedding a PDF file with specific width and height. In this example, the width is set to 600 pixels and the height to 800 pixels.

[pdf-embedder url="http://example.com/path-to-your-pdf.pdf" width="600" height="800" /]

Displaying a PDF file with a custom toolbar position. The toolbar can be set to appear at the top, bottom, both or none. In this example, the toolbar is set to appear at the top of the PDF.

[pdf-embedder url="http://example.com/path-to-your-pdf.pdf" toolbar="top" /]

Embedding a PDF file with a fixed toolbar. This means the toolbar will remain visible even when the user scrolls through the PDF. In this example, the toolbar is set to be fixed.

[pdf-embedder url="http://example.com/path-to-your-pdf.pdf" toolbarfixed="on" /]

Displaying a PDF file with a custom title. This title will be displayed instead of the default title derived from the URL. In this example, the title is set to “My Custom Title”.

[pdf-embedder url="http://example.com/path-to-your-pdf.pdf" title="My Custom Title" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'pdf-embedder', array($this, 'pdfemb_shortcode_display_pdf') );

Shortcode PHP function:

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

		$atts = apply_filters('pdfemb_filter_shortcode_attrs', $atts);

		if (!isset($atts['url'])) {
			return '<b>PDF Embedder requires a url attribute</b>';
		}

		$url = $atts['url'];

		$this->insert_scripts();

        // Get defaults

        $options = $this->get_option_pdfemb();

		$width = isset($atts['width']) ? $atts['width'] : $options['pdfemb_width'];
		$height = isset($atts['height']) ? $atts['height'] : $options['pdfemb_height'];

		$extra_style = "";
		if (is_numeric($width)) {
			$extra_style .= "width: ".$width."px; ";
		}
        elseif ($width!='max' && $width!='auto') {
            $width = 'max';
        }

		if (is_numeric($height)) {
			$extra_style .= "height: ".$height."px; ";
		}
		elseif ($height!='max' && $height!='auto') {
			$height = 'max';
		}

		$toolbar = isset($atts['toolbar']) && in_array($atts['toolbar'], array('top', 'bottom', 'both', 'none')) ? $atts['toolbar'] : $options['pdfemb_toolbar'];
        if (!in_array($toolbar, array('top', 'bottom', 'both', 'none'))) {
            $toolbar = 'bottom';
        }

		$toolbar_fixed = isset($atts['toolbarfixed']) ? $atts['toolbarfixed'] : $options['pdfemb_toolbarfixed'];
        if (!in_array($toolbar_fixed, array('on', 'off'))) {
            $toolbar_fixed = 'off';
        }

		$title = isset($atts['title']) && $atts['title'] != '' ? $atts['title'] : $this->make_title_from_url($url);

		$pdfurl = $this->modify_pdfurl($url);
		$esc_pdfurl = esc_attr($pdfurl);

		$returnhtml = '<a href="'.$esc_pdfurl.'" class="pdfemb-viewer" style="'.esc_attr($extra_style).'" '
						.'data-width="'.esc_attr($width).'" data-height="'.esc_attr($height).'" ';

		$returnhtml .= $this->extra_shortcode_attrs($atts, $content);

		$returnhtml .= ' data-toolbar="'.$toolbar.'" data-toolbar-fixed="'.$toolbar_fixed.'">'.esc_html( $title ).'<br/></a>';

		if (!is_null($content)) {
			$returnhtml .= do_shortcode($content);
		}
		return $returnhtml;
	}

Code file location:

pdf-embedder/pdf-embedder/core/core_pdf_embedder.php

Conclusion

Now that you’ve learned how to embed the PDF Embedder Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

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