Google Docs Shortcode Tutorial

Shortcodes are handy WordPress functions, they allow you to quickly call information or to add a specific function in posts and pages.

Today I’m going to show you how to take advantage of the excellent Google Docs and allow users to read PDF, DOC and PPT files directly from your site without having to download to their machines.

All the following snippets take place within the trusty old functions.php file, so backup before you start!

PDF Files

Add to functions.php

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

$content.'</a>';
}
add_shortcode('pdf', 'pdf_short');

In a post or page you would use shortcode like this:

[pdf href="http://domain.com/link/to/file.pdf"]PDF Link Here[/pdf]

DOC Files

Add to functions.php

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

$content.'</a>';
}
add_shortcode('doc', 'doc_short');

In a post or page you would use shortcode like this:

[doc href="http://domain.com/link/to/file.doc"]DOC Link Here[/doc]

PPT Files

Add to functions.php

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

$content.'</a>';
}
add_shortcode('ppt', 'ppt_short');

In a post or page you would use shortcode like this:

[ppt href="http://domain.com/link/to/file.ppt"]PPT Link Here[/ppt]

Other File Types

Google Docs supports a few other filetypes so by altering the code you can use these shortcodes for even more filetypes if you need to.

You Might Also Like