Skip to content

Instantly share code, notes, and snippets.

@mrwweb
Created January 13, 2017 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrwweb/4e396b3211729a54b8d0c96254aa64f3 to your computer and use it in GitHub Desktop.
Save mrwweb/4e396b3211729a54b8d0c96254aa64f3 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Google PDF Viewer Shortcode
Description: Display a PDF in the browser with Google's PDF viewer. Usage: [pdf]http://example.org/some-public-document.pdf[/pdf]. Can specify optional width and height attribrutes like this [pdf width="100" height="100"]http://example.org/a-very-tiny-document.pdf[/pdf]
Version: 1.0
Author: Mark Root-Wiley
Author URI: https://mrwweb.com
*/
/* a shortcode for embedding PDFs via Google Docs viewer */
function nten_pdf_shortcode( $atts, $content = '' ) {
if( '' === $content || '.pdf' !== substr( $content, -4 ) ) {
return;
}
$atts = shortcode_atts( array(
'width' => '600',
'height' => '775'
), $atts, 'pdf' );
return '<iframe src="https://docs.google.com/viewer?url=' . esc_url( $content ) . '&embedded=true" style="width:' . intval( $atts['width'] ) . 'px; height:' . intval( $atts['height'] ) . 'px;" frameborder="0" class="us-embedded-pdf"></iframe>';
}
add_shortcode( 'pdf', 'nten_pdf_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment