Embed Any Document Shortcode

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

Before starting, here is an overview of the Embed Any Document Plugin and the shortcodes it provides:

Plugin Icon
Embed Any Document – Embed PDF, Word, PowerPoint and Excel Files

"Embed Any Document is a handy WordPress plugin that allows you to seamlessly embed PDFs, Word documents, PowerPoints, and Excel files directly into your posts and pages. Keep your content engaging and informative with this versatile tool."

★★★★✩ (91) Active Installs: 70000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [embeddoc]

Embed Any Document [embeddoc] Shortcode

The Embed Any Document shortcode is a versatile tool that allows embedding of documents into a WordPress site. The shortcode is: [embeddoc]. It supports multiple document providers like Google and Microsoft. The shortcode allows customization of width, height, download options, and language settings. It also supports cache control and viewer selection. It ensures responsive design and provides a preloader for Google viewer. The shortcode also handles AMP endpoints for better mobile experience.

Shortcode: [embeddoc]

Parameters

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

  • url – The link to the document you want to embed.
  • drive – Not used in the given code.
  • width – The width of the embedded document.
  • height – The height of the embedded document.
  • language – Language for the document viewer interface.
  • text – Text for download link, if enabled.
  • viewer – The document viewer provider (google or microsoft).
  • download – Controls who can download the document.
  • cache – If ‘off’, the document is always fetched from the server.

Examples and Usage

Basic example – Display a document using a URL with default settings

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

Advanced examples

Display a document with custom width and height

[embeddoc url="http://example.com/path-to-your-document.pdf" width="600px" height="400px" /]

Display a document with a specific viewer (Google or Microsoft) and language

[embeddoc url="http://example.com/path-to-your-document.pdf" viewer="microsoft" language="fr" /]

Display a document with a download link for logged in users

[embeddoc url="http://example.com/path-to-your-document.pdf" download="logged" /]

Display a document with a custom download text

[embeddoc url="http://example.com/path-to-your-document.pdf" download="alluser" text="Download the document" /]

Display a document without caching in Google viewer

[embeddoc url="http://example.com/path-to-your-document.pdf" viewer="google" cache="off" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'embeddoc', array( $this, 'embed_shortcode' ) );

Shortcode PHP function:

function embed_shortcode( $atts ) {
		$embed            = '';
		$durl             = '';
		$default_width    = $this->sanitize_dims( get_option( 'ead_width', '100%' ) );
		$default_height   = $this->sanitize_dims( get_option( 'ead_height', '100%' ) );
		$default_provider = get_option( 'ead_provider', 'google' );
		$default_download = get_option( 'ead_download', 'none' );
		$default_text     = get_option( 'ead_text', __( 'Download', 'embed-any-document' ) );
		$show             = false;
		$shortcode_atts   = shortcode_atts(
			array(
				'url'      => '',
				'drive'    => '',
				'width'    => $default_width,
				'height'   => $default_height,
				'language' => 'en',
				'text'     => $default_text,
				'viewer'   => $default_provider,
				'download' => $default_download,
				'cache'    => 'on',
			),
			$atts
		);

		wp_enqueue_style( 'awsm-ead-public' );
		wp_enqueue_script( 'awsm-ead-public' );

		if ( isset( $shortcode_atts['url'] ) && ! empty( $shortcode_atts['url'] ) ) :
			// AMP.
			$is_amp = function_exists( 'is_amp_endpoint' ) && is_amp_endpoint();

			$durl      = '';
			$viewer    = $shortcode_atts['viewer'];
			$providers = self::get_all_providers();

			if ( ! in_array( $viewer, $providers, true ) ) {
				$viewer                   = 'google';
				$shortcode_atts['viewer'] = 'google';
			}

			$is_browser_viewer = false;
			if ( $shortcode_atts['viewer'] === 'browser' ) {
				// fallback for Browser viewer.
				$is_browser_viewer = true;
				$viewer            = 'google';
				// AMP handling.
				if ( $is_amp ) {
					$is_browser_viewer = false;
				}
			}

			if ( $this->allowdownload( $shortcode_atts['viewer'] ) ) {
				if ( $shortcode_atts['download'] === 'alluser' || $shortcode_atts['download'] === 'all' ) {
					$show = true;
				} elseif ( $shortcode_atts['download'] === 'logged' && is_user_logged_in() ) {
					$show = true;
				}
			}
			$url = esc_url( $shortcode_atts['url'], array( 'http', 'https' ) );
			if ( $show ) {
				$filedata = wp_remote_head( $shortcode_atts['url'] );
				$filesize = 0;
				if ( ! is_wp_error( $filedata ) && isset( $filedata['headers']['content-length'] ) ) {
					$filesize = $this->human_filesize( $filedata['headers']['content-length'] );
				} else {
					$filesize = 0;
				}
				$file_html = '';
				if ( $filesize ) {
					$file_html = ' [' . $filesize . ']';
				}
				$durl = '<p class="embed_download"><a href="' . esc_url( $url ) . '" download >' . $shortcode_atts['text'] . $file_html . ' </a></p>';
			}

			if ( $shortcode_atts['cache'] === 'off' && $viewer === 'google' ) {
				if ( $this->url_get_param( $url ) ) {
					$url .= '?' . time();
				} else {
					$url .= '&' . time();
				}
			}

			$iframe_src = '';
			switch ( $viewer ) {
				case 'google':
					$embedsrc   = '//docs.google.com/viewer?url=%1$s&embedded=true&hl=%2$s';
					$iframe_src = sprintf( $embedsrc, rawurlencode( $url ), esc_attr( $shortcode_atts['language'] ) );
					break;

				case 'microsoft':
					$embedsrc   = '//view.officeapps.live.com/op/embed.aspx?src=%1$s';
					$iframe_src = sprintf( $embedsrc, rawurlencode( $url ) );
					break;
			}

			$iframe_style_attrs = array();
			$doc_style_attrs    = array(
				'position' => 'relative',
			);
			if ( $this->check_responsive( $shortcode_atts['height'] ) && $this->check_responsive( $shortcode_atts['width'] ) && ! $is_browser_viewer ) {
				$iframe_style_attrs = array(
					'width'    => '100%',
					'height'   => '100%',
					'border'   => 'none',
					'position' => 'absolute',
					'left'     => '0',
					'top'      => '0',
				);

				$doc_style_attrs['padding-top'] = '90%';
			} else {
				$iframe_style_attrs = array(
					'width'  => $shortcode_atts['width'],
					'height' => $shortcode_atts['height'],
					'border' => 'none',
				);
				if ( $this->in_percentage( $shortcode_atts['height'] ) ) {
					$iframe_style_attrs['min-height'] = '500px';
				}
			}

			$enable_preloader = ! $is_amp && $viewer === 'google';

			if ( $enable_preloader ) {
				$iframe_style_attrs['visibility'] = 'hidden';
			}

			$data_attr = '';
			if ( $is_browser_viewer ) {
				$data_attr = sprintf( ' data-pdf-src="%1$s" data-viewer="%2$s"', esc_url( $shortcode_atts['url'] ), esc_attr( $shortcode_atts['viewer'] ) );

				$doc_style_attrs = array_merge( $doc_style_attrs, $iframe_style_attrs );
				unset( $doc_style_attrs['visibility'] );
			}

			$iframe_style = self::build_style_attr( $iframe_style_attrs );
			$iframe       = sprintf( '<iframe src="%s" title="%s" class="ead-iframe" %s></iframe>', esc_attr( $iframe_src ), esc_html__( 'Embedded Document', 'embed-any-document' ), $iframe_style );

			if ( $enable_preloader ) {
				$iframe = '<div class="ead-iframe-wrapper">' . $iframe . '</div>' . self::get_iframe_preloader( $shortcode_atts );
			}

			$doc_style = self::build_style_attr( $doc_style_attrs );
			$embed     = sprintf( '<div class="ead-preview"><div class="ead-document" %3$s>%1$s</div>%2$s</div>', $iframe, $durl, $doc_style . $data_attr );
		else :
			$embed = esc_html__( 'No Url Found', 'embed-any-document' );
		endif;

		/**
		 * Customize the embedded content.
		 *
		 * @since 2.6.0
		 *
		 * @param string $embed The embedded content.
		 * @param array $shortcode_atts The shortcode attributes.
		 */
		$embed = apply_filters( 'awsm_ead_content', $embed, $shortcode_atts );

		return $embed;
	}

Code file location:

embed-any-document/embed-any-document/awsm-embed.php

Conclusion

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