I’ve been geeking out with the new post formats feature in WordPress 3.1 for the past few weeks and I’ve decided to make a theme that supports all of the different formats. I’m just about have through them and I would like to share a neat trick that I used to enable users to post images a bit faster.
The Image Post Format
The WordPress Codex suggests that an image format is:
A single image. The first <img > tag in the post should be considered the image. Alternatively, if the post consists only of a URL, that will be the image URL and the title of the post (post_title) will be the title attribute for the image.
This is pretty straight forward, and I was intrigued by the possibility of using the content to hold only a url to an image. If you could just copy and paste the url to an image into the post content, it would really speed up the workflow.
I won’t go into too many details but will say that this has passed all of my tests. If you’ve got a minute to test it out, please let me know your thoughts!
<?php
/*
* Filter content for posts having an image format.
*
* This filter will allow authors to post only a url to an image
* in the post content and have the image display properly in the
* theme as an img tag.
*
* An "alt" attribute will always be generated for each image. An
* author can specify the value of the alt attribute by creating
* a custom field with a key of "mfields_alt". If no custom field
* is available, an empty string will be used.
*
* The title attribute of the img tag will be populated by the value
* produced by get_the_title(). In the event that there is no post
* title available no title attribute will be printed.
*
* Child themes may customize the attributes generated for each image
* by registering a custom function for the
* 'mfields-image-format-attributes' filter. An example follows:
*
* This filter will add a class of "mysite" to all images hosted at
* the same domain as WordPress is installed on.
*
* <code>
* function mytheme_filter_image_format_attributes( $atts, $src ) {
* if ( false !== strstr( $src, site_url( '', 'http' ) ) ) {
* if ( isset( $atts['class'] ) ) {
* $atts['class'] = (array) $atts['class'];
* }
* $atts['class'][] = 'mysite';
* }
* return $atts;
* }
* add_filter( 'mfields-image-format-attributes', 'mytheme_filter_image_format_attributes', 10, 2 );
* </code>
*
* @param string Post content.
* @return string HTML img tag if all conditions are met, unfiltered content otherwise.
*
* @since 1.0
*/
function mfields_filter_content_for_image_format( $content ) {
if ( 'image' == get_post_format() ) {
$src = esc_url( $content );
$gis = @getimagesize( $src );
if ( ! empty( $src ) && is_array( $gis ) ) {
$attributes = array();
$post_id = get_the_ID();
$title = get_the_title();
$defaults = array(
'alt' => get_post_meta( $post_id, 'mfields_alt', true ),
'width' => $gis[0],
'height' => $gis[1]
);
if ( ! empty( $title ) ) {
$defaults['title'] = $title;
}
$filtered = apply_filters( 'mfields-image-format-attributes', $defaults, $src, $gis );
$atts = array_merge( $defaults, $filtered );
foreach( $atts as $name => $value ) {
if ( in_array( $name, array( 'id', 'class', 'alt', 'title', 'height', 'width', 'longdesc', 'style' ) ) ) {
if ( 'class' == $name && is_array( $value ) ) {
$value = implode( ' ', $value );
}
$attributes[] = $name . '="' . esc_attr( $value ) . '"';
}
}
$content = '<img src="' . $src . '" ' . implode( ' ', $attributes ) . '>';
}
}
return $content;
}
add_filter( 'the_content', 'mfields_filter_content_for_image_format', 0 );
?>



This is quicker than manually uploading the image- but it feels hackish because it just isn’t the right interface for this type of format.
I’d take a look at this plugin by WooThemes: http://www.woothemes.com/2010/10/wootumblog/. They set up a separate quick press interface to make something similar work.
I think it would also work very nice in a custom post type with a custom ui.
I don’t know of I would consider this hackish really, I believe that that I have followed the guidelines set out by WordPress pretty strictly.
The link you posted looks pretty sweet and I would hope that WordPress would adopt a similar approach in a future version, I believe that the dashboard is the appropriate place to create formatted posts.
IMHO A modified quick edit dashboard widget would be best left to a plugin that could be shared across all themes supporting post formats.
I think when you choose a post format “image”, the metaboxes should change to “Paste or upload image” rather than “Content”. Your code nails the functionality- the only reason it feels hackish is because the actual WordPress UI hasn’t evolved to support it in an ideal way.
What would be good, is to take the URL, and have it go grab a copy of the image, upload it as an attachment (like the Importer feature does automatically), then amend the post content to create an image tag with the local version. Now *that* would be cool.
Agreed! That would be pretty wicked. Wish I had time to look into it more. I’m still focused on new years resolution #1 which is to release a theme into the theme directory. If you get inspired and create this. Please post a link to your solution here. I think people would be interested such functionality.
Totally agree with you that a better user interface would really make post formats shine. The post formats codex page hints at UI considerations for the quote format. I think that this is something that we will see addressed in the future. Either through core or someone creating a plugin.
Hey, thanks for sharing this code!
Here is a link to a plugin which is supposed to grab copies of external images, save them as post attachments, and then point to the local files:
http://www.bbqiguana.com/wordpress-plugins/add-linked-images-to-gallery/
Haven’t tested it yet, just thought I’d share.
Sweet! Thanks for a link to the plugin! This is definitely something that I want to check out.
Here is a somewhat similar link to another plugin with a slightly different angle. It can grab and upload a YouTube, Vimeo, Daily motion url thumbnail and set it as featured image. May provide some good insite…
http://wordpress.org/extend/plugins/auto-featured-image/
Luke, Thanks for posting that! Looks awesome for sure. Will have to try iy out. That sounds like a perfect solution to the video post format.