Google Docs Shortcode Tutorial

Tutorials | By Paul Maloney | 15 November 2010 | Comments 18

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.

Paul Maloney

Paul Maloney is a UK based web designer/developer and runs the design agency Tropica. He particularly enjoys using and working with WordPress and has a keen interest in typography. You can also follow Paul on Twitter.

Visit Paul Maloney's website

18 Comments

  • November 15, 2010
    • November 15, 2010
  • November 15, 2010
    • November 15, 2010
  • November 15, 2010
    • November 15, 2010
  • November 16, 2010
  • November 17, 2010
    • December 9, 2010
  • December 9, 2010
  • December 15, 2010
  • December 15, 2010
  • January 18, 2011

Leave A Comment.