Enable Google Docs Online Viewer for PDF files in WordPress

Sometimes, we need to attach a pdf file on the blog to add reports or other documents for readers. In general, people add a link to PDF file that opens PDF file in the browser on clicking. This way is fine but what if you want to embed PDF document within the blog post. By embedding the PDF, you will give visitors a way to check the document within your blog post. There is no need to leave the blog post. You can add Google Docs Online Viewer to open PDF files. If you are willing to add this to your blog, you can do this with simple code modification or WordPress Plugin.
In this post, we will be discussing how to enable Google Docs online viewer for PDF files in WordPress.
Also Read: How to Embed Google Trends Graph in WordPress Posts And Pages
Enable Google Docs Online Viewer for PDF files in WordPress
Like other things, you can also do this with either manual way or with plugin way. It depends on your personal choice. In this post, I am adding both ways.
Manual way
Add this code to functions.php file.
function pdflink($attr, $content) { if ($attr['href']) { return '<a class="pdf" href="http://docs.google.com/viewer?url=' . $attr['href'] . '">'.$content.'</a>'; } else { $src = str_replace("=", "", $attr[0]); return '<a class="pdf" href="http://docs.google.com/viewer?url=' . $src . '">'.$content.'</a>'; } } add_shortcode('pdf', 'pdflink');
Now use this shortcode to add PDF file in post or pages.
[pdf href="https://www.usethistip.com/uploads/fileupload.pdf"]My PDF file[/pdf]
This will open the PDF file in the Google Docs viewer within the blog post. So, visitors will not leave your blog post even if they are reading the PDF file. It helps in reducing the bounce rate and increasing the user experience.
Plugin way
If you are not comfortable with code modification, you can add the same functionality via a plugin. Google Code Embedder is a nice plugin that lets you embed Google Docs Online Viewer for multiple file formats. You can use it to embed PDF, DOC, PPT, XLS, PSD and many more supported formats.
You can also use Google Doc Embedder WordPress plugin. This plugin also offers same things.
I am not going into details of plugins and working of plugins. I am sure you can easily understand how to use these plugins. Download and install these plugins in your WordPress blog. If you have any trouble while using these plugins, do not forget to comment below and ask me. I am always free to reply and help you.
Final words
Now, you know how to embed Google Docs document viewer in your WordPress for showing PDF and other files within the blog posts. Try any of the given ways as per your need.
The concept is simple. Google Docs viewer allow us to see any document just by passing the URL. See the URL below:
https://docs.google.com/viewer?url=URL_OF_PDF
Now you only need to pass the URL of the PDF file to view that file using Google Document viewing. Not just PDF, you can also pass other file formats. This document viewer supports many other file formats.
Note: I also want to confirm that these ways are only to use Google Docs PDF viewers to display the hosted PDF files. It is not for displaying PDF files saved on Google Docs. So, keep this in mind and do not confuse yourself.
If you have anything to ask or suggest, you can comment below to share your thoughts with us.
Code Snippet: WpSnipp
Leave a comment
Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.