How to embed Google Docs PDF by creating a shortcode

PDF is one of the most popular file formats on the Internet. Since it is designed to work on all platforms, this Portable Document Format (PDF) quickly became a standard way of displaying documents. You can easily create a PDF and view it locally, you can share a document online or you can even embed one directly into your website. We’re not going to talk much about the format itself, but if you want to learn more about it, you can check out Wikipedia pages which provide tons of information about the PDF.

Whether if it’s an e-book, a catalog of your products which you want to show online or simply a text document, you can allow people to view that PDF right on your WordPress powered website. Instead of linking to a file which a user would have to download and then view locally, you can harvest the power of Google Docs and embed a PDF. By doing so, you will create a PDF viewer directly in your post, page, widget or practically anywhere you want.

Google Docs PDF viewer you’re about to embed is very simple. It will show the document including only buttons to scroll through pages, a zoom button as well as the button that will open the document in a new window. If you ask us, that’s more than enough, but if you would like more from your PDF viewer, you will want to see best plugins for displaying PDFs.

Before you can embed the shortcode, you will first have to register one.

Embed Google Docs PDF viewer:

  1. Open functions.php
  2. Copy and paste the following code:
  3. function pdf_function($attr, $url) {
    extract(shortcode_atts(array(
    'width' => '800',
    'height' => '600'
    ), $attr));
    return '<iframe src="http://docs.google.com/viewer?url=' . $url . '&embedded=true" style="width:' .$width. '; height:' .$height. ';">Your browser does not support iframes</iframe>';
    }
    add_shortcode('pdf', 'pdf_function');
    
  4. Save changes

Now that the code is safely stored into the functions file, you are ready to use the shortcode and show that PDF to everyone. To use the shortcode, you will need to set width and height for the viewer (this is obviously a must have parameter since you can use the shortcode on many different places) and of course provide a link to the document:

[pdf]https://ithemes.com/wp-content/uploads/downloads/2015/10/getting-started-with-WordPress-ebook.pdf[/pdf]

That’s it. From now on, you can use your [pdf] shortcode anytime you want; don’t forget to change the URL leading to your document and change dimensions if needed. If you’re about to use the shortcode on a daily basis, you should consider creating a button which will add the shortcode.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

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