X

How To Create a Google Docs Shortcode for PDF Documents

Snippets by IsItWP

Are you looking for a way to create a Google Docs shortcode for PDF documents? This snippet will make the [pdf] shortcode link PDF documents to the Google Docs online viewer.

Instructions:

  1. All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin.
  2. Create the shortcode like this: [pdf href="http://example.com/file.pdf"]My PDF file[/pdf], using your own URL and text.
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');

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 15 Best Content Marketing Tools and Plugins for WordPress.

Comments  Leave a Reply

  1. Documents Sample March 26, 2011 at 6:46 am

    I tried once and its working,thanks for sharing

    1. no problem, glad you like it!

Add a Comment Cancel reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!