Enable google docs shortcode for .pdf documents

Adding this snippet to the functions.php of your wordpress theme will setup [pdf] shortcode to link PDF documents to Google docs online viewer.

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');
[pdf href="http://wpsnipp.com/uploads/file.pdf"]My PDF file[/pdf]

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

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!