Category Archives: Blogging

Multi-line Captions in WordPress

As my wife and I were each posting our daily photos this afternoon, she asks me to help her put a two line caption under one of her photos. Being somewhat knowledgable about such things, I tell her that I’d be happy to help. And what should have been a very simple task, turned into an hour and a half of messing around and re-writing the captioning capabilities in WordPress. Let me start with a little background. First, let me acknowledge that WordPress is a great blogging platform for text. And I have seen some really beautiful photography blogs created on this platform. But what most people don’t realize is that these photo-blogging themes have a ton of custom code. Out of the box, WordPress is still absolutely awful with regards to text mixed with media. And I do realize that they’ve been constantly updating things, but it’s frankly still a crude environment. The multi-line captions are a good example. While WordPress 2.5 added the ability to have captions, instead of building it into the user interface and allowing the user / writer to create content to be used as a caption, they instead decided to have the photo import interface add an odd caption shortcode which WordPress will later replace with actual HTML. What this means is that unless you want to modify WordPress core files (a very bad idea since you’d have to do it every time you upgraded WordPress), you’re stuck trying to fix captions within the bounds of their already lacking system. So, without further rambling, here’s the long and the short of fixing the captioning system (a little bit).

Current Implementation (WordPress 2.5+)

Before fixing the captions, we need to understand exactly how it works out of the box.  When you go to insert a photo into a post, if you type anything in the caption dialog, WordPress will insert an image surrounded by a “caption” shortcode.  It goes through and replace this caption shortcode with actual HTML when it’s requested by a reader. However, instead of taking a block of text and including it, all it takes is one lousy string that can’t have anything but text.

Allowing Multiline Captions

Due to the existing implementation, there is really only so much we can work with. We need to take the caption and break it into multiple lines, but remember, WordPress won’t store more than a string for the caption. To fix this, I decided that I’d insert line breaks into the caption based on a text string — in my case, I chose the “|” symbol as I don’t ever see it being used in a caption. You can replace it with whatever you like. To override the existing caption code, edit your theme’s functions.php file and add the following code (note, clicking on the left-most button in the top right of the code box will open the source in an editor — perfect for copying and pasting).

Please note, this will not work when inserting images in the WordPress Gallery format.

/* ********************************************* 
Function to allow multi-line photo captions. This function will 
split captions onto multiple lines if it detects a "|" (pipe) symbol. 
********************************************** */ 
/* Override existing caption shortcode handlers with our own */ 
add_shortcode('wp_caption', 'multiline_caption'); 
add_shortcode('caption', 'multiline_caption'); 

/* Our new function */ 
function multiline_caption($attr, $content = null) 
{ 
    extract(shortcode_atts(array( 'id' => '', 
                                  'align' => 'alignnone', 
                                  'width' => '', 
                                  'caption' => '' ), $attr)); 
    if ( 1 > (int) $width || empty($caption) ) 
        return $content; 
    if ( $id ) $id = 'id="' . esc_attr($id) . '" '; 

    $new_caption = str_replace("|", " ", $caption); 

    return '<div ' . $id . 'class="wp-caption ' . 
           esc_attr($align) . '" style="width: ' . 
           (10 + (int) $width) . 'px">' . 
           do_shortcode( $content ) . 
           '<p class="wp-caption-text">' . 
           $new_caption . </p></div>'; 
} 

With this done, all that was left was to edit the caption of the desired photo with something like this: “First line of caption.|Second line of caption”.

Additional Resources

In searching the web for a solution, I found everything but what I needed. However, I did find some good resources for WordPress captions in general.

I’ve been listed on Cool Photoblogs

I was looking around at some of the really good photoblogs out there on the net and I ran across some which had won the Photoblog Awards for different years.  While I’m really not thinking that my humble place here in cyberspace is worthy of such an honor, I did want to point out that they did accept my submission here.

So, if you’re enjoying my work, or just want to go see other really good photography, check out the above link and either vote for me, or look around!

Photoblog Awards

What is SEO and why do I need it?

Looking around at numerous other blogs I frequent, it becomes painfully clear just how little many people understand about SEO, or Search Engine Optimization.  In this situation, ignorance is not bliss as these blogs are potentially sacrificing a large number of readers who can’t find the content contained within.

What is SEO?

SEO, or Search Engine Optimization is the process by which the author of a web page (or blog in our case) organizes and formats their content to make it more accessible to search engines such as Google, Yahoo, or Bing.  This sounds relatively simple, right?  Well, let’s start off by making it clear that there is no such thing as a blog with perfect SEO simply because there is no 100% definition of exactly what “perfect SEO” really entails.  However, there are a few simple things that can go a long way to making sure that your blog is at least getting indexed by search engines. Continue reading

2010 New Year’s Resolutions

So 2009 is almost over, and while I know I should have expected the year to end (as they always do), I’m somehow more surprised than usual.  Apparently, I’d gotten a little too comfortable with writing 2009 on things so it is now time to force me to change (or else scribble out a lot of dates).  If the past is any indicator, it will take me until June before I figure out what’s going on with regards to documenting the correct year.

With that said, I’m trying to actually make some New Year’s Resolutions this year.  You see, I’m on a roll from last year having successfully accomplished my single resolution to make no resolutions.  While it may not be a huge undertaking, it’s one which I took seriously and am proud to say I’ve generally succeeded.  I like to enjoy the successes in my life – no matter how small and insignificant they may seem.

So this year, I’m going to actually try to market Digital Notions.  If you’re read my previous article entitled Rookie Blogging Mistakes, you’ll soon realize this is one of my biggest hurdles I’m facing with the success of this site.  In an effort to do this, I’m going to be undertaking a huge task – posting at least one photo every day.

I’m not going to set any limits on the subject matter of these daily photos.  They may be simple snippets of my daily life.  They may be photos of which I’m particularly proud.  Or, they may be some experiments that either did or didn’t come out correctly.  Regardless, I’m hoping to see a progression throughout the year.  Overall, I’m trying to grow as a photographer.  We’ll see how it goes.

In addition to Photo A Day plan, I’m going to try to write more.  You see, as much as I enjoy photography, I also enjoy writing.  I plan to not only write more helpful articles for fellow bloggers (especially about the Photo A Day undertaking), but perhaps write some more creative pieces.  I don’t want topics to stray too far from the purpose of this site, but I do want this site to evolve into something that is uniquely me.

So there you have it!  If you have any ideas for topics to write about, or any ideas for daily photo themes, leave a comment and let me know!