Post Format Tools
The Post Format Tools extension is a set of tools created to standardize how themes handle post formats. WordPress defines post formats as a way to “standardize” some theme features and to make theme “portable” from theme to theme. This extension merely makes that process easier for theme developers by offering a set of a tools to work with. Some parts are automatic while others are manual.
Post Format Tools is very much a work in progress (though it’s stable). I’m sure it’ll grow and become more refined over time. The best thing you can do as a theme developer is to dive straight into the code and provide any feedback you can. This tutorial isn’t a 100% complete, in-depth guide. Think of it more as an overview of the extension, at least until we can finalize everything into a specific set of standards for post formats.
Adding support for Post Format Tools
This extension will be automatically loaded if your theme supports post formats. So, adding support for the post-formats
theme feature will load it.
The following is an example of supporting post formats.
add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
As a theme developer, it is your responsibility to fully understand how WordPress post formats work before using the Post Formats Tools extension. Without that knowledge, this extension will not be useful to you.
Automatic features
Several of the features work out of the box for specific post formts with no need for further input from you.
Post format archive titles
By default, WordPress has no plural versions of the post format strings, which is a shame since it makes sense for most post format archives to be represented by a plural version (you don’t have one post in an archive; you have many).
So, what this feature does is automatically filter single_term_title
(used for term archive titles) and replaces it with a plural version of the title.
Chat post format
Chat posts are tough. Fortunately, this community came up with an awesome solution to this. Chat posts are automatically formatted for easy styling via a theme’s stylesheet.
A user only need to input their chats in the following format (common format for chats). The script handles the rest.
John: foo
Mary: bar
John: foo 2
The following is an example of the output of a chat post in HTML form.
<div id="chat-transcript-562" class="chat-transcript">
<div class="chat-row chat-speaker-1">
<div class="chat-author chat-author-john vcard"><cite class="fn">John</cite>:</div>
<div class="chat-text"><p>foo</p></div>
</div><!-- .chat-row -->
<div class="chat-row chat-speaker-2">
<div class="chat-author chat-author-mary vcard"><cite class="fn">Mary</cite>:</div>
<div class="chat-text"><p>bar This is an example of a WordPress page, you could edit this to put information about yourself or your site so readers know where you are coming from.</p></div>
</div><!-- .chat-row -->
<div class="chat-row chat-speaker-1">
<div class="chat-author chat-author-john vcard"><cite class="fn">John</cite>:</div>
<div class="chat-text"><p>foo 2 What: is: this:</p></div>
</div><!-- .chat-row -->
<div class="chat-row chat-speaker-3">
<div class="chat-author chat-author-smith vcard"><cite class="fn">Smith</cite>:</div>
<div class="chat-text"><p>Aye, matey!</p></div>
</div><!-- .chat-row -->
<div class="chat-row chat-speaker-4">
<div class="chat-author chat-author-joe vcard"><cite class="fn">Joe</cite>:</div>
<div class="chat-text"><p>Huh?</p></div>
</div><!-- .chat-row -->
<div class="chat-row chat-speaker-2">
<div class="chat-author chat-author-mary vcard"><cite class="fn">Mary</cite>:</div>
<div class="chat-text"><p>bar 2</p></div>
</div><!-- .chat-row -->
</div><!-- .chat-transcript -->
As you can see, there are plenty of classes for you to style. Here’s some basic CSS to get you started.
/* Wrapper for entire chat transcript. */
.chat-transcript { }
/* Individual section of the chat. */
.chat-row { }
/* Individual speaker in the chat. */
.chat-speaker-xxx { }
/* Chat author. */
.chat-author { }
.chat-author cite { }
/* Chat text. */
.chat-text { }
If you really want a full-fledged tutorial on how just this feature works, check out my blog post on chats.
Quote post format
Sometimes users input a <blockquote>
around their quotes in “quote” posts. Sometimes they don’t. So, theme developers need an easy way to figure this out. But, with this script, they don’t have to worry. It’s taken care of.
This script will check if the user added a <blockquote>
in their post content. If not, the entirety of the post will be considered a quote and wrapped with the <blockquote>
element.
Link post format
This is more of a user feature than a developer feature. This functionality basically runs the WordPress make_clickable()
function over the content of “link” posts. The purpose of this is to simply turn any URLs into hyperlinks if the user didn’t do so.
Available functions
The following functions are functions that you may use in your theme templates and will be useful for particular post formats.
post_format_tools_post_has_content()
The post_format_tools_post_has_content()
function checks if the current post in The Loop has any content saved. It will return true
or false
based on the result.
You may also optionally input a post ID as the first parameter to check for a specific post’s content.
post_format_tools_url_grabber()
The post_format_tools_url_grabber()
function checks if the current post in The Loop has any links within it. If it does, it returns the first link. If not, it returns the post permalink.
This function is useful in “link” posts if you want to replace the post permalink in The Loop with the link the user has input in the post content.
post_format_tools_get_image_attachment_count()
The post_format_tools_get_image_attachment_count()
function returns the number of image attachments for the current post in The Loop. This function was created for use with the “gallery” post format so that theme developers could output the number of image attachments for gallery posts in their templates.
post_format_tools_get_video()
The post_format_tools_get_video()
function checks if a post contains an embedded video within it. If it does, the function returns the full embed HTML for output to the screen. This function is useful for displaying the video embed for “video” posts in places where you might normally display a post excerpt instead of the full post content (for example, archive pages).
You may also optionally specificy a width and height (it uses the user’s media settings by default). Example:
echo post_format_tools_get_video( array( 'width' => 500, 'height' => 300 ) );
function get_the_feature_caption() {
global $post;
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_image = get_posts(array(‘p’ => $thumbnail_id, ‘post_type’ => ‘attachment’));
if ($thumbnail_image && isset($thumbnail_image[0])) {
$caption = ‘‘.$thumbnail_image[0]->post_excerpt.’‘;
}
return $caption;
}