Google Doc Embedder Shortcode

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

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

Plugin Icon
Google Doc Embedder

"Google Doc Embedder is a powerful WordPress plugin that allows you to embed Google documents directly into your pages or posts, enhancing your site's content visibility."

★★★★✩ (91) Active Installs: 30000+ Tested with: 4.9.24 PHP Version: false
Included Shortcodes:
  • [gview]

Google Doc Embedder [gview] Shortcode

The Google Document Embedder shortcode allows embedding Google documents into WordPress posts. The shortcode, [gview], uses the function ‘gde_do_shortcode’ to extract attributes like file, profile, save, width, and height from the shortcode. It retrieves the requested profile data, applies default settings if not defined, and validates the file. It then creates a viewer link and an optional download link based on the user permissions.

Shortcode: [gview]

Parameters

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

  • file – The URL of the document to be embedded.
  • profile – Identifier for the profile settings to be used.
  • save – Controls if a download link should be shown.
  • width – Defines the width of the embedded document.
  • height – Defines the height of the embedded document.

Examples and Usage

Basic example – Embed a Google Document using the file URL

[gview file='http://yourwebsite.com/path/to/document.pdf']

Advanced examples

Embed a Google Document by specifying the dimensions of the embedded document

[gview file='http://yourwebsite.com/path/to/document.pdf' width='500px' height='300px']

Embed a Google Document by specifying a profile ID and enabling the save option

[gview file='http://yourwebsite.com/path/to/document.pdf' profile='2' save='1']

Embed a Google Document by specifying a profile ID, dimensions, and enabling the save option

[gview file='http://yourwebsite.com/path/to/document.pdf' profile='2' width='500px' height='300px' save='1']

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( 'gview', 'gde_do_shortcode' );

Shortcode PHP function:

function gde_do_shortcode( $atts ) {
	global $gdeoptions;

	extract( shortcode_atts( array (
		'file' => '',
		'profile' => 1, // default profile is always ID 1
		'save' => '',
		'width' => '',
		'height' => ''
	), $atts ) );
	
	// get requested profile data (or default if doesn't exist)
	$term = $profile;
	if ( is_numeric( $term ) ) {
		// id-based lookup
		if ( ! $profile = gde_get_profiles( $term ) ) {
			gde_dx_log("Loading default profile instead");
			if ( ! $profile = gde_get_profiles( 1 ) ) {
				return gde_show_error( __('Unable to load requested profile.', 'google-document-embedder') );
			} else {
				$pid = 1;
			}
		} else {
			$pid = $term;
		}
	} else {
		// name-based lookup
		if ( ! $profile = gde_get_profiles( strtolower( $term ) ) ) {
			gde_dx_log("Loading default profile instead");
			if ( ! $profile = gde_get_profiles( 1 ) ) {
				return gde_show_error( __('Unable to load requested profile.', 'google-document-embedder') );
			} else {
				$pid = 1;
			}
		} else {
			$pid = $profile['profile_id'];
		}
	}
	
	// use profile defaults if shortcode override not defined
	if ( $save !== "0" ) {
		if ( empty( $save ) ) {
			$save = $profile['link_show'];
		}
	}

	if ( empty( $width ) ) {
		$width = $profile['default_width'];
	}
	if ( empty( $height ) ) {
		$height = $profile['default_height'];
	}
	$lang =  $profile['language'];

	// tweak the dimensions if necessary
	$width = gde_sanitize_dims( $width );
	$height = gde_sanitize_dims( $height );
	
	// add base url if needed
	if ( ! preg_match( "/^http/i", $file ) ) {
		if ( substr( $file, 0, 2 ) == "//" ) {
			// append dynamic protocol
			if ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ) {
				$file = "https:" . $file;
			} else {
				$file = "http:" . $file;
			}
		} elseif ( isset( $profile['base_url'] ) ) {
			// not a full link, add base URL if available
			if ( substr( $file, 0, 1 ) == "/" ) {
				// remove any preceding slash from doc (base URL adds it)
				$file = ltrim( $file, '/' );
			}
			$file = $profile['base_url'] . $file;
		}
	}
	
	// capture file details
	$fn = basename( $file );
	$fnp = gde_split_filename( $fn );
	
	// check for missing required field
	if ( ! $file ) {
		return gde_show_error( __('File not specified, check shortcode syntax', 'google-document-embedder') );
	}
	
	// file validation
	if ( $gdeoptions['error_check'] == "no" ) {
		$force = true;
	} else {
		$force = false;
	}
	$status = gde_validate_file( str_replace( " ", "%20", $file ), $force );
	
	if ( ! isset( $code ) && ! is_array( $status ) && $status !== -1 ) {
		// validation failed
		$code = gde_show_error( $status );
	} elseif ( ! isset( $code ) ) {
		// validation passed or was skipped
		
		// check for max filesize
		$viewer = true;
		if ( $gdeoptions['file_maxsize'] > 0 && isset( $status['fsize'] ) ) {
			$maxbytes = (int) $gdeoptions['file_maxsize'] * 1024 * 1024;
			if ( $status['fsize'] > $maxbytes ) {
				$viewer = false;
			}
		}
		
		// check for failed secure doc
		if ( empty( $file ) ) {
			$code = gde_show_error( __('File name is empty', 'google-document-embedder') );
		} else {
		
			// which viewer?

			$lnk = "//docs.google.com/viewer?url=" . urlencode( $file  ) . "&hl=" . urlencode($lang);

			$lnk .= "&embedded=true";

			// build viewer
			if ( $viewer == false ) {
				// exceeds max filesize
				$vwr = '';
			} else {
				$vwr = '<iframe src="%U%" class="gde-frame" style="width:%W%; height:%H%; border: none;" scrolling="no"></iframe>';
				$vwr = str_replace("%U%", $lnk, $vwr);
				$vwr = str_replace("%W%", esc_attr($width), $vwr);
				$vwr = str_replace("%H%", esc_attr($height), $vwr);
			}
			
			// show download link?
			$allow_save = false;
			if ( $save == "all" || $save == "1" ) {
				$allow_save = true;
			} elseif ( $save == "users" && is_user_logged_in() ) {
				$allow_save = true;
			}

			if ( $allow_save ) {
				// build download link
				$linkcode = '<p class="gde-text"><a href="%LINK%" class="gde-link"%ATTRS%>%TXT%</a></p>';
				$linkcode = str_replace( "%LINK%", $file, $linkcode );
				
				// fix type
				$ftype = strtoupper( $fnp[1] );
				if ( $ftype == "TIF" ) { 
					$ftype = "TIFF";
				}
				
				// link attributes
				$attr[] = gde_ga_event( $file ); // GA integration
				$linkcode = str_replace("%ATTRS%", implode( '', $attr ), $linkcode);
				
				// link text
				if ( empty( $profile['link_text'] ) ) {
					$profile['link_text'] = __('Download', 'google-document-embedder');
				}
				
				$dltext = str_replace( "%FILE", $fn, $profile['link_text'] );
				$dltext = str_replace( "%TYPE", $ftype, $dltext );
				$dltext = str_replace( "%SIZE", gde_format_bytes( $status['fsize'] ), $dltext );
				
				$linkcode = str_replace( "%TXT%", $dltext, $linkcode );
			} else {
				$linkcode = '';
			}
			
			// link position
			if ( $profile['link_pos'] == "above" ) {
				$code = $linkcode . "\n" . $vwr;
			} else {
				$code = $vwr . "\n" . $linkcode;
			}
		}
	}
	
	return $code;
}

Code file location:

google-document-embedder/google-document-embedder/gviewer.php

Conclusion

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