Create custom post status mesasges in admin

Adding this snippet to the functions.php of your wordpress theme will let you add custom status messages for each post.


I recently wrote this as a way for a client to display a custom messages for each post an author creates. In this case a post could have a message as rejected, error, source, final etc. you can change the messages just bellow the comment “Array of custom status messages” just make sure the class names are the same names. You can change them after the comment “change color of messages bellow”. I was going to setup a custom metabox however for this snippet I inserted the select menu into the “publish” metabox after the “Schedule for” option.

 

 

add_filter( 'display_post_states','custom_post_state');
function custom_post_state( $states ) {
global $post;
$show_custom_state = get_post_meta( $post->ID, '_status' );
if ( $show_custom_state ) {
$states[] = __( '<span class="custom_state '.strtolower($show_custom_state[0]).'">'.$show_custom_state[0].'</span>' );
}
return $states;
}
add_action( 'post_submitbox_misc_actions', 'custom_status_metabox' );
function custom_status_metabox(){
global $post;
$custom  = get_post_custom($post->ID);
$status  = $custom["_status"][0];
$i   = 0;
/* ----------------------------------- */
/*   Array of custom status messages            */
/* ----------------------------------- */
$custom_status = array(
'Spelling',
'Review',
'Errors',
'Source',
'Rejected',
'Final',
);
echo '<div class="misc-pub-section custom">';
echo '<label>Custom status: </label><select name="status">';
echo '<option class="default">Custom status</option>';
echo '<option>-----------------</option>';
for($i=0;$i<count($custom_status);$i++){
if($status == $custom_status[$i]){
echo '<option value="'.$custom_status[$i].'" selected="true">'.$custom_status[$i].'</option>';
}else{
echo '<option value="'.$custom_status[$i].'">'.$custom_status[$i].'</option>';
}
}
echo '</select>';
echo '<br /></div>';
}
add_action('save_post', 'save_status');
function save_status(){
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ return $post->ID; }
update_post_meta($post->ID, "_status", $_POST["status"]);
}
add_action( 'admin_head', 'status_css' );
function status_css() {
echo '<style type="text/css">
.default{font-weight:bold;}
.custom{border-top:solid 1px #e5e5e5;}
.custom_state{
font-size:9px;
color:#666;
background:#e5e5e5;
padding:3px 6px 3px 6px;
-moz-border-radius:3px;
}
/* ----------------------------------- */
/*   change color of messages bellow            */
/* ----------------------------------- */
.spelling{background:#4BC8EB;color:#fff;}
.review{background:#CB4BEB;color:#fff;}
.errors{background:#FF0000;color:#fff;}
.source{background:#D7E01F;color:#333;}
.rejected{background:#000000;color:#fff;}
.final{background:#DE9414;color:#333;}
</style>';
}

 

If you like this snippet share the love – tweet, Facebook like or try the new google plus 1.

Leave a comment