Skip to content

Instantly share code, notes, and snippets.

@jonathanfann
Last active February 1, 2018 15:58
Show Gist options
  • Save jonathanfann/c965d37f1145de5b74c38da54ec507df to your computer and use it in GitHub Desktop.
Save jonathanfann/c965d37f1145de5b74c38da54ec507df to your computer and use it in GitHub Desktop.
PDF Embed Shortcode (For Wordpress); just include this in your functions.php file
<?php // TODO: put this in your functions.php file and use on a page like [pdf]/path-to-your/file.pdf[/pdf] ?>
<?php
function pdf_embed_shortcode($attrs, $content) {
$pdfPath = $content;
if ( wp_is_mobile() ) {
$htmlStuff =
'<div style="text-align:center;"><p id="mobilefallback">This browser does not support native PDF embeds or you are on a mobile device. Please <a href="' . $pdfPath . '" target="_blank">download</a> the PDF instead.</p>
<p class="pdfDownload"><a class="btn btn-primary btn-md" href="' . $pdfPath . '" target="_blank">Download</a></p></div>';
} else {
$htmlStuff =
'<div id="pdfViewer">
<object class="pdfObject" data="' . $pdfPath . '" type="application/pdf" width="100%" height="100%">
<iframe src="' . $pdfPath . '" width="100%" height="100%" style="border: none;">
This browser does not support PDF embeds.
Please <a href="' . $pdfPath . '" target="_blank">Download PDF</a> instead.
</iframe>
</object>
<p class="pdfDownload"><a class="btn btn-primary btn-md" href="' . $pdfPath . '" target="_blank">Download</a></p>
</div>';
}
return $htmlStuff;
}
add_shortcode( 'pdf', 'pdf_embed_shortcode' );
// minor css and you got yourself a nice little PDF with fallback and a neat lil' download button
// example use: [pdf]/wp-content/uploads/2018/01/just_another.pdf[/pdf]
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment