
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]
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.
Really great tip buddy!!
thanks mate, glad you enjoyed
Why not create a filetype dependent function?
function gdocs_view($attr, $content) {
return ‘‘.
$content.’‘;
}
add_shortcode(‘gdocs-view’, ‘gdocs_view’);
I initially though of doing that, but I thought by breaking it down it might provide further insight to the usage of shortcodes.
Cheers mate
Would this require a login to google to view the documents?
Nope! No need to even have a Google account, just fires right up
right on time, i am working with google docs now for manuals for the clients
lol perfect timing!!
really useful tip bro
no worries, glad you enjoyed
A great post Paul which also highlights how to create shortcodes in WordPress it goes beyond a tutorial for displaying documents in Google Docs
Very cool! I am yet to see this shortcode. Good work Paul.
Thanks Robert and Brady, glad you both found it useful
no login require
that’s great, thanks Robert & Brady. Let me immediate implement this.
Trackbacks
Pages
Categories
Archives
Blogroll
Twitter
Popular Posts
Twitter Followers
Recent Posts
Recent Comments