WordPress Wednesdays: Build your own simple shortcodes

Geek Factor: 3

Last week, we briefly touched on using shortcodes directly in your WP theme. In this week’s WordPress Wednesday, we’re going to cover how to build your own simple shortcodes and add them to your WordPress theme, and why you may want to add them to your site.

Use shortcodes for special formatting

Shortcodes can be an easy way to insert specific HTML around a snippet of content; this can be handy for bits of content like pullquotes or tips that require special formatting to style in a specific way, but that can be tricky for most authors to add without knowing basic HTML.

Shortcodes can be an easy way to insert specific HTML around a snippet of content.
- Laurel Fulford

To create a simple pullquote shortcode, like the example to the right, first you need to know what HTML you would like to output, and what your users will need to edit in the quote themselves. In the example below, I’ve just wrapped the shortcode in a <div> and allowed the user to add the pullquote’s content, and, optionally, the source of the quote and what side of the content the pullquote will be aligned to.

For the above example, the following code is added to the theme’s functions.php file — before making any edits to your theme, first back up your files:

PHP
1
2
3
4
5
6
7
8
9
10
11
function pullquote_shortcode($atts) {
    extract(shortcode_atts(array(
        'content' => '',
        'author' => '',
        'align' => 'right',
        ), $atts));
    
    if(!empty($author)) $author = '<div class="quote-author"> -'.$author.'</div>';    
    return '<div class="pullquote align'.$align.'">'.$content.$author.'</div>';
}
add_shortcode('pullquote','pullquote_shortcode');

If the ‘author’ attribute is present, the value is then wrapped in a div with the class ‘quote-author’, allowing even more specific styling to the name.

Then, in the editor, you can add a pullquote, specifying the quote’s content, author and alignment if desired. The below shortcode will output the pullquote code that you see at the top of this section:

XHTML
1
[pullquote author="Laurel Fulford" content="Shortcodes can be an easy way to insert specific HTML around a snippet of content."]

Last but not least, add a bit of style to your CSS for the .pullquote class to change the font, colours, size, background, etc., and you’re good to go!

Using shortcodes to prevent HTML from being stripped

There is some code that WordPress will strip out of the editor when you toggle from HTML to Visual mode; this includes HTML tags like iframe. You can use shortcodes to create a work-around, and also make adding iframes less daunting to less-techy authors.

To create an iframe shortcode, the following php would be added to your theme’s functions.php file. In it, there are a number of attributes an author may want to control in the iframe; most have a default value already set, so all they absolutely need to define is the url to be displayed:

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
function iframe_shortcode($atts, $content=null) {
extract(shortcode_atts(array(
        'url' => '',
        'scrolling' => 'no',
        'width' => '100%',
        'height' => '500',
        'frameborder' => '0',
        'marginheight' => '0',
        ), $atts));
  
    return '<iframe src="'.$url.'" title="" scrolling="'.$scrolling.'" width="'.$width.'" height="'.$height.'" frameborder="'.$frameborder.'" marginheight="'.$marginheight.'">'.$content.'</iframe>';
}
add_shortcode('iframe','iframe_shortcode');

To use the iframe shortcode in the editor, you would just need to add the following to your post:

XHTML
1
[iframe url="http://www.stemlegal.com" width="540" height="300" scrolling="yes"]

The output for the above tag would look like this:

These are just two of many helpful uses for shortcodes — can you think any others you would add to your WordPress website? Share them in the comments below!

Local Search and hReview

Geek Factor: 2

I have become intrigued by something called hReview, which I saw a reference to on the Whitespark blog the other day. Basically, it’s a microformat (like hCard) used for reviews of, well, pretty much anything that can be reviewed: books, movies, restaurants, music, etc.

The interesting thing is that you can use put testimonials on your website in hReview format, and Google may use these in rich snippets for Places listings. This may be a way for businesses whose customers don’t typically leave reviews on sites like Yelp, Citysearch, Insiderpages, etc., to round out their Google Places listings with legitimate reviews.

A useful concept for local search and one we’re looking forward to experimenting with! See more at Google Places Optimization Blog, Google Webmaster Tools, and Microformats.org.

Presentation: ‘Better Content by Design’

Geek Factor: 1

An Event Apart recently posted the video of a presentation by Kristina Halvorson titled ‘Message and Medium: Better Content by Design.’


 
Here at Stem, we’ve been delving deeper into crafting the right message for clients (and ourselves!), and finding the right channels to deliver that message. Definitely an interesting watch if you’ve been struggling with that question yourself!

Via Jeffery Zeldman.

WordPress Wednesdays: Use shortcodes in theme files

Geek Factor: 3

Filed under ‘something I wish I knew sooner’: this week’s WordPress Wednesday covers a simple little built-in function that can save you a lot of mucking around in WordPress:
do_shortcode().

In short (heh), a shortcode in WordPress is a macro that can be used in the WYSIWYG editor. You add a shortcode to your post, and when WordPress renders the page, it swaps the shortcode for whatever code it’s a placeholder for, passing along any settings that have been included. WordPress has built-in shortcodes, like for the gallery, and shortcodes are also used in many plugins.

In the case of the WordPress gallery, if you wanted to insert one into a post, you would add the following shortcode to the editor:

[gallery]

Shortcodes, like HTML tags, can also be passed attributes — the gallery, like many shortcodes, has a number of settings you can tweak to customize it:

[gallery size="medium" columns="5" order="ASC"]

If you wanted to insert the gallery right into the WordPress theme rather than a post or page, you would just need to wrap the shortcode in the do_shortcode() function:

<?php do_shortcode('[gallery size="medium"]'); ?>

I didn’t know about this function until quite recently; my work-around was to add the shortcode to a WordPress page, and then query that page in the theme to display the content. Not very efficient!

Have you made any recent discoveries about WordPress that you wish you had known sooner? Please share them in the comments!

Amazing mobile phone user manual

Vitamins, a London based design studio, created a unique and amazing user manual for Samsung phones, specifically targeted at less tech-savvy users.

From their site:

Rather than design a dumbed down “special phone” we decided to look at the problem in a different way. Maybe existing phones don’t need to be changed, maybe we should change the way we learn how to use them instead.

Ideas like this really challenge my perception of how helpful good design can actually be.

(via @danielmall)

Repairing SEO’s Tarnished Reputation

Three recent blog posts on the same theme have me nodding my head in agreement. The consensus is that the kind of sleazy, lazy SEO that some folks use to artificially optimize website rankings has got to stop. Content written expressly for rankings and ads is of zero use to the user, and actually makes search engines worse at search.

Nathaniel Mott at Appstorm thinks SEO is breaking the web:

“People have gotten so good at SEO that they can make a result appear for something that doesn’t even exist.

This has taken search from naturally powerful and thorough to a complete crapshoot. Now you’re going to have to click through several results that seem perfect before you can find an item that actually has what you’re looking for. When this happens once it’s a pain – when it happens a dozen times, it’s a flaw with the system.”

With tongue firmly in cheek, Sonia Simone at Copyblogger says there’s a “new” SEO secret weapon: stop catering to search engines, and start focusing on site quality – revolutionary, eh? ; ) We are advised, “Don’t take shortcuts, they take too long.”

And what is the true cost of “perfect law firm SEO“? Jeremy Hessing-Lewis at Skunkworks says that in highly competitive legal niches, “unfortunately, the trend is to sacrifice everything for the sake of SEO.” He drafts some “perfect seo” website content for a fictional firm, which would be hilarious in its effect if I hadn’t seen a hundred actual sites just like it.

It’s nice to see these sentiments put so frankly. Maybe it’s time to stop using the term “search engine optimization”, if this is what the term has come to mean. Maybe we should call it “content visibility optimization” or “quality and utility optimization” or some other mouthful. I’m only half-serious here, but whether we use the term SEO or not, collectively, we need to repair its tarnished reputation.

Properly and ethically done, SEO is about making content and websites as easily found and as useful to the reader as possible. To sum up, Sonia puts it nicely:

“…if you aren’t building a site that’s worth reading (and that’s therefore worth sharing in social media, and worth linking to), the most brilliant shortcut in the world will take you away from where you want to go.”

I’m curious – what do you think? Can SEO be reclaimed? Ideas for a better term to replace “search engine optimization”?  Feel free to share your thoughts in the comments.

Great gallery of interface details

Geek Factor: 2

Little Big Details is an online gallery showcasing some amazing details in website and software user interfaces. Anyone can submit screenshots of neat details they’ve noticed on their favourite sites, and the gallery makes for some fascinating browsing and inspiration.

The examples in the gallery range from (literally!) invisible usability tweaks, like the iPad’s ‘phantom keys’ when you use the split keyboard, to ones that show the dev team’s sense of humour, like IMDB’s rating for ‘This Is Spinal Tap‘ going up to 11. There are even examples of ‘little details’ in website’s code, like Tumblr’s ascii logo hidden in their source HTML.

If you have any interest at all in web or software design, you’ll get a kick out Little Big Details!

WordPress Wednesdays: Stop WP from linking images by default

Geek Factor: 3

This week’s WordPress Wednesday post covers a topic near and dear to my heart: undoing WordPress’s annoying habit of linking images when they’re inserted into the editor.

By default, when you add an image to a WordPress post or page using the ‘Insert Media’ tool in the editor, WordPress will wrap the image in a link that simply leads to the image itself. It is possible to remove the link when adding the image, if you actually remember to do it: I use WordPress all the time and still forget, like with the logo in last week’s jQuery post.

This comment by Matt Weibe on Andrew Norcross’s blog offered up a handy bit of PHP code. As with all template tweaks, make sure to back up your files before making any changes, then paste this code into your theme’s functions.php file:

PHP
1
2
3
4
add_action('pre_option_image_default_link_type', 'always_link_images_to_none');
function always_link_images_to_none() {
return 'none';
}

You will still be able to add links to images when using the WordPress ‘Insert Media’ tool; it just won’t be done automatically.

Another option for changing default behavior is available on Aizat Faiz’s blog, and requires you to use WordPress’s hidden options screen. Editing this page is like tampering with the options table in your WordPress database; in other words, we highly recommend not changing any other values while you’re in there.

After logging in to your WordPress site, go to [your-website-url]/wp-admin/options.php and you will see a long list of WP interface options, listed alphabetically. The option you are looking for is ‘image_default_link_type‘:

The default setting is ‘file’, which inserts a link directly to your image file in the uploads folder. Removing the value so the field is blank will remove the default linking functionality. (The third setting for the field is ‘post’, which will insert a link around the image to an attachment page with the image as the only content; not much better than the default ‘file’ linking.)

After the ‘image_default_link_type‘ has been updated, scroll to the bottom of the page and click ‘Save Changes’.

Do you have any WordPress pet peeves? Share them in the comments below!

iPads for Lawyers & Law Firms

On a recent episode of LXBN TV, Brett Burney of Macs in Law shared a couple of his top apps for lawyers using iPads, and talked about how IT departments are adapting to this new piece of hardware that’s popping up in law firms with increasing frequency.

Burney mentions Good Reader as essential for viewing PDFs online, and Documents to Go as his top choice for editing Word docs on the iPad.

The interview was filmed at LegalTech New York 2012 after Burney’s presentation with David Neeson, “iLove for the iPad”. See detailed notes of the presentation at iLibrarian.

Learn jQuery for free with Tuts+

Geek Factor: 3

Tuts+ is currently offering a free web course: 30 Days to Learn jQuery. Once you sign up, you are emailed a link to a new video daily. Each video is about 15 minutes long (or shorter), and the series starts with the bare-bones basics, then slowly moves into more advanced material.

jQuery is the JavaScript library that’s used by WordPress, so it’s a great choice for using with websites built with WP, as you can link right to the jQuery files that it is already loading.

If you’ve been looking at learning jQuery from scratch, I recommend checking out the series!