This is very similar to @mrwweb's answer, but a little bit more fleshed out. We're also not going to overload taxonomies in order to get our post class, but do what you wanted initially: use a custom meta box.
There is a built in template tag called post_class
. Like almost every other function in WordPress you can control its output via a filter.
Step one: add the meta box.
This is done via add_meta_box
. If you're wondering what the arguments are, you can head on over to the codex -- the docs are very good. The thing to be concerned about is the third argument which specifies a callback function -- the callback is what renders the HTML. To add the meta box we'll hook into add_meta_boxes_post
and call add_meta_box
.
Here's the whole deal:
<?php
add_action('add_meta_boxes_post', 'wpse52122_meta_box');
/*
* Adds a meta box on the post editing screen.
*
* @uses add_meta_box
*/
function wpse52122_meta_box()
{
add_meta_box(
'wpse52122-box',
__('Add Custom Style', 'wpse52122'),
'wpse52122_meta_box_cb',
'post',
'side',
'low'
);
}
/*
* Callback function for the meta box added above. This is what renders the
* boxes HTML.
*
* @uses get_post_meta
*/
function wpse52122_meta_box_cb($post)
{
$meta = get_post_meta($post->ID, '_wpse52122_style', true);
wp_nonce_field('wpse52122_nonce', 'wpse52122_nonce', false);
echo '<p>';
echo '<label for="wpse52122_style">' . __('Edited?', 'wpse52122') . '</label> ';
echo '<input type="checkbox" name="wpse52122_style" id="wpse52122_style" ' .
checked($meta, 'on', false) . ' />';
echo '</p>';
}
Now we need to actually save that stuff when the post is saved. You hook into save_post
to do that:
<?php
add_action('save_post', 'wpse52122_save_post');
/*
* Hooked in `save_post` this is the function that will take care of saving the
* checkbox rendered above
*
* @uses update_post_meta
* @uses wp_verify_nonce
* @uses current_user_can
*/
function wpse52122_save_post($post_id)
{
// no auto save
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
// can the current user edit this post?
if(!current_user_can('edit_post', $post_id))
return;
// Do we have our nonce?
if(!isset($_POST['wpse52122_nonce']) ||
!wp_verify_nonce($_POST['wpse52122_nonce'], 'wpse52122_nonce')) return;
// if the the box is set, save meta key with 'on' as the value.
$val = isset($_POST['wpse52122_style']) && $_POST['wpse52122_style'] ?
'on' : 'off';
update_post_meta($post_id, '_wpse52122_style', esc_attr($val));
}
Finally, hook into post_class
to add your 'edited' class. Your hooked function will get three arguments, we're concerned with the first and last: the array of post classes and the post id respectively.
<?php
add_filter('post_class', 'wpse52122_post_class', 10, 3);
/*
* Hooked into `post_class` this function adds the class 'edited' to the
* post_class array if the meta value `_wpse52122_style` is set to 'on'
*
* @uses get_post_meta
* @return array The Post classes
*/
function wpse52122_post_class($classes, $class, $post_id)
{
if('on' == get_post_meta($post_id, '_wpse52122_style', true))
{
$classes[] = 'edited';
}
return $classes;
}
Done! All that seems like quite a bit of work, however. So why not just hook into post_class
and compare the post_created_gmt
and post_modified_gmt
?
<?php
add_filter('post_class', 'wpse52122_post_class_alt', 11, 3);
/*
* Filter the post_class and add the class edited if the post_date_gmt &
* post_modified_gmt don't match up.
*
* @uses get_post
* @return array The post classes
*/
function wpse52122_post_class_alt($classes, $class, $post_id)
{
$post = get_post($post_id);
if(!$post) return $classes;
$created = strtotime($post->post_date_gmt);
$mod = strtotime($post->post_modified_gmt);
if($mod > $created)
{
$classes[] = 'edited-alt';
}
return $classes;
}
Here is all of that as a plugin.