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].

WordPress Trick: Output Content In Two Columns Separated By <!–more–> Tag [Updated]

Need to split up your WordPress content in two columns without too much of a hassle? How about if you could do that by simply inserting the <!--more--> to indicate where you want the split? Well that’s what you can do using this trick.

Replace the_content(); in your theme with this code. That probably means editing your themes “post loop” (usually in page.php, single.php or postloop.php):

$more = 0;
$content_1 = wpautop(do_shortcode(get_the_content('')));
$more = 1;
$content_2 = wpautop(do_shortcode(get_the_content('',true)));

// check if the more tag is used at all
if ($content_1 == $content_2) {
	echo $content_1;
} else {
	echo '
' . $content_1 . '
' . $content_2 . '
'; }

Paste this in your themes style.css:

#column_one {
	float: left;
	width: 50%;
}
#column_two {
	float: right;
	width: 50%;
}

From now on you can use the “More” tag to switch your content into columns.

[Update]: Fixed problem with paragraphs.

Sliding Doors In WordPress

A default WordPress page menu is built like this:

'0', 'sort_column'=>'menu_order'));
?>

– which outputs a plain menu, <li><a href=" ... ">Menu Item</a></li> and so on.

If you want to use the sliding doors CSS technique, however, you need more markup. So do this:

]*)>\]*)>(.*?)\<\/a>@i', '
$3', wp_page_menu(array('echo'=>false,'depth'=>'0', 'sort_column'=>'menu_order')) );
?>

– which’ll output this a more CSS friendly markup: <li><a href=" ... "><span>Menu Item</span></a></li>.

WordPress 3

Work has started on version 3 of WordPress, and while I’m looking forward to custom post types (so I can build “Add News”, “Add Employee”, “Add Video” sections), the big news is the merge with WordPress Multi User. Rumors are, it’ll be renamed WordPress Multi Site.

Image Symlinks, quick bulk image upload for WordPress [Updated August 2011]

Image-Symlinks.png

Image Symlinks is born of a desire to speed up and simplify WordPress post images, while retaining quality. Very briefly, it works like this:

  1. Upload large source image
  2. Use Symlink-image-button to insert image into post
  3. Decide width and/or height of inserted image

How’s that different from using the built-in WordPress image upload and insertion mechanism? To put it shortly, step 2 and 3 have switched place. WordPress scales your images according to preset sizes when you upload, and doesn’t allow you to change those sizes without changing a global setting and uploading again. Image Symlinks creates the image cache on demand, and according to either a global setting, or individual image settings.

This is smarter than it sounds, and the reason for which I created Zenphoto Short-tags in the first place, so I’ve written about it at length before.

Features

Image Symlinks is powered by TimThumb, so some of its features are care of the great gentlemen Ben Gillbanks and Darren Hoyt. Symlink Images does the following:

  • Lets you use the built-in WordPress image uploader, but insert them via Symlink Images for extra scaling options
  • Decide on a custom upload directory, and get access to a custom and super userfriendly Symlinks image uploader
  • Easily browse your WordPress images or Symlinks-uploaded images and insert with one click (guaranteed to be faster than the bulky WordPress interface)
  • Cache is created on-demand, and tidied up automatically
  • Transparent PNGs keep their 24bit transparency when scaled

A lot more features are on the drawing board. Read on.

Built-in Image Uploader

Image-Symlinks-upload.png

Image Symlinks uses Uploadify to upload images. Upon upload, you can insert image directly, or  you can go browse your repository.

Using the Symlinks uploader, you by-pass the WordPress database entirely and deal only with clean image files in one directory and cache files in a separate directory.

If you want your images to be saved in the WordPress media library, use the built-in WordPress uploader, but insert those images using Image Symlinks. That way you both get access to the new 2.9 crop and edit features, and Image Symlinks’ advanced cache and scaling features.

Image-Symlinks-insert.png

Options

Image-Symlinks-options.png

Image Symlinks currently allows you to specify a default width and a default CSS class. The above image, for instance, has no CSS class (but you could specify “alignright” if you want your images right-aligned by default), and a default width of 600px.

Inherently Extensible

Since Image Symlinks built-in uploader by-passes the WordPress database, it allows you to easily combine your WordPress with your custom gallery CMS. Simply specify your custom upload directory to point to the gallery album folder and you’re all set. I’ve integrated Image Symlinks with ZenPhoto, for instance, so any image I upload is also added to my ZenPhoto photostream.

In addition to this, all images are auto-linked by default, pointing to their larger versions with a simple hyperlink. This is optimal for search engine optimization, and makes it super-easy to combine with a Lightbox solution.

How To

Simply upload and insert your image using the custom Image Symlinks media button. If you want to be manual, the syntax is:

[raw]
Image File.png
[/raw]

Upon activation of the plugin, you should make the cache directory writable by the webserver. By default, the built-in uploader is disabled, and you use Image Symlinks only to insert images. If you want to specify your own upload directory, that also has to be writable.

Download

Download Image Symlinks (currently beta) from the WordPress Repository

Future Features

Image Symlinks is currently a rather early release. I’m taking a page from Google and dogfeeding myself, using it on this blog. So far it’s super-snappy and very stable, but not quite feature-complete. Any feedback is welcome, but here’s what’s planned:

  • Image Symlinks should offer to create custom directories for you
  • Option and shortcode attribute to disable auto-linking
  • GUI for inserting more than one image at a time
  • GUI for adjusting width/height of inserted image
  • Shortcode attributes and GUI for cropping images
  • Gallery widget
  • Recent images widget

Aside from this, I’d like to simplify the whole installation process to the point at which it’s “activate and you’re done”.

Look for more features next year.

Updates

  • April 2010: Bulk insertion! The image insertion dialog no longer closes whenever you click to insert an image, but only tells you the image is now inserted. Much faster.
  • May 2011, 0.7: TimThumb has been updated, and the cache directory is now created automatically in the wp-content folder (provided it’s writable). This should make first installation, as well as subsequent updates way easier.
  • 0.8: Large polish update. Plugin now translatable, and new beta shortcode: latestimages, which shows your last x images, like this:
  • 0.8.5: Security fixes.

WordPress QuickTip: Pagination

For some websites, WordPress’ builtin pagination feature (<!--nextpage-->) is supremely useful. Storing here, as much for my own posterity as for yours, a few tricks to get it up and running.

Restore the “Nextpage” button in the Visual editor by adding the following to your functions.php:

/**
 * Restore Nextpage Visual Editor button
 */
add_filter('mce_buttons','nextpage');
function nextpage($btns) {
	$pos = array_search('wp_more', $btns, true);
	if ($pos !== false) {
		$temp_btns = array_slice($btns, 0, $pos+1);
		$temp_btns[] = 'wp_page';
		$btns = array_merge($temp_btns, array_slice($btns, $pos+1));
	}
	return $btns;
}

Convert the pagination to “next” and “prev” buttons by adding this to your WordPress “post loop”:

&after=
&next_or_number=next&nextpagelink='.__("Next").'&previouspagelink='.__("Previous").''); ?>