WordPress Trick: Create A Google Viewer PDF Shortcode

Want your PDFs to view through Google Reader? Wrap it in a shortcode like this: [raw][pdf href="http://noscope.com/photostream/albums/various/no.pdf"]Link to a PDF[/pdf][/raw]. Drop the following in your functions.php:

function pdflink($attr, $content) {
	return '<a class="pdf" href="http://docs.google.com/viewer?url=' . $attr['href'] . '">'.$content.'</a>';
}
add_shortcode('pdf', 'pdflink');

[Update]: Here’s an updated shortcode function which allows you to also embed PDFs using this syntax: [raw][pdf=http://noscope.com/photostream/albums/various/no.pdf]Link to a PDF[/pdf][/raw]. Note how it’s just a tad shorter, and you don’t have to remember the “href” parameter, though you can still use it.

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');

Also, here’s an example PDF link using the above code: [pdf href="http://noscope.com/photostream/albums/various/no.pdf"]Link to a PDF[/pdf].

Responses to “WordPress Trick: Create A Google Viewer PDF Shortcode”

  1. 分享 http://tinyurl.com/ygztt2e (WordPress Trick: Create A Google Viewer PDF Shortcode) http://plurk.com/p/3zevv0

  2. Theo says:

    Awesome update, thanks for the snippet !

  3. gofasefer says:

    wonderful code tanx
    the function closed braket is not comming after the semicolon it may need to chane the code at list i may case it not working

    }
    add_shortcode(‘pdf’, ‘pdflink’);

    —————————

    add_shortcode(‘pdf’, ‘pdflink’);
    }
    ——–

  4. eran says:

    Hi,
    Will users be able to download the PDF file? I’m looking for a secured way to publish my stuff (for free) but without allowing users to download the PDF file, only view it.
    Can this help me?

    Thanks a lot,

    Eran

  5. eran says:

    Hi,
    Thank you so much for your quick reply.
    May there’s a flash plugin or something?do you know if Scribd allows disabling downloading and printing the documents? I’m trying to go over the support page without any luck so far :)

Reply