X

Update: Create custom post status messages in admin

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


This is an updated version of the (Create custom post status messages in admin) adding some additional features. Only users with the “publish_posts” capability can change the status while everyone sees what the status (if set) is. 1. Restricting add status with the capability publish_posts 2. Remove status notification selecting none.


Code Snippet:


add_filter( 'display_post_states', 'custom_post_state' );
function custom_post_state( $states ) {
global $post;
$show_custom_state = get_post_meta( $post->ID, '_status' );
// We are using "None" as a way to disable this feature for the current post.
if ( $show_custom_state && $show_custom_state[0] != 'None' ) $states[] = '<span class="custom_state ' . strtolower( $show_custom_state[0] ) . '">' . $show_custom_state[0] . '</span>';
return $states;
}
add_action( 'admin_head', 'status_css' );
function status_css()
{
echo '
<!– Styling of Custom Statuses –>
<style type="text/css">
.custom{border-top:solid 1px #e5e5e5;}
.custom_state{
font-size:9px;
color:#666;
background:#e5e5e5;
padding:3px 6px 3px 6px;
-moz-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;
border-radius:3px;
}
.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>';
}
// Only those with the capability should be able to change things.
if ( current_user_can( 'publish_posts' ) ) {
// Insert our "Custom Status" into the Post Publish Box
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;
// Available Statuses
$custom_status = array( 'None', 'Spelling', 'Review', 'Errors', 'Source', 'Rejected', 'Final' );
echo '
<div class="misc-pub-section custom">Custom status:
<select name="ourstatus">';
for ( $i = 0; $i < count( $custom_status ); $i++ ) {
echo '<option value="' . $custom_status[$i] . '"';
if ( $status == $custom_status[$i] ) echo ' selected="selected"';
echo '>' . $custom_status[$i] . '</option>';
}
echo '</select></div>';
}
// Save
add_action( 'save_post', 'save_status' );
function save_status( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id;
update_post_meta( $post_id, "_status", $_POST["ourstatus"] );
}
}

My first post show me some love 🙂

Comments  Leave a Reply

  1. Hi guys, hi Gabriel, i just wondering about Status label translation to different language? If i change them i must to change css labels too in order to have style results, but if the language is Cyrillic? Any idea how to separate the code in order to give him a different from same label css style?

  2. This and the other “draft” snipp no longer work with WordPress 3.3.1

  3. I was just playing with this a bit further and it’s utterly terrific. What would make it really really amazing, is if you could assign more than one status to a post. So say the post had spelling and content issues both would show up.

    1. Hi Zach ya that would be a great idea, ill see about doing an updated version of the snippet for something along that lines. Could be very cool!

  4. Zach Shallbetter July 27, 2011 at 10:21 pm

    This conflicts with the “Higlight Post_States” function. Causes the status to loose it’s layout and break a line.

    1. Hi Zach,
      Yes this is true, I posted an updated version for you on paste bin that you can use. I did not change much to the snippet just after the comment on line 05.
      ” // We are using “None” as a way ”

      This is just a simple update and should be tweaked a little better but if you place the following code into the functions.php and replace both snippets this will solve the issue. I put both snippets into pastebin for you.

      http://pastebin.com/zNHEZMKR

      1. Thanks!

      2. Hi. Thanks for this as I’ve also made use of this change to display both the default and custom post states. I have a minor styling issue though. I’m not too fond of the comma that is placed in between 2 post states. Is there any way to remove this at all? Thanks for your time and great code!

        1. Ill look into this and the other suggestions for an updated version in the near future.

  5. Zach Shallbetter July 27, 2011 at 10:21 pm

    This conflicts with the “Higlight Post_States” function. Causes the status to loose it’s layout and break a line.

  6. Any particular reason -webkit-border-radius was left out of custom_state in the CSS?

    1. No just forgot to add in -webkit- and border-radius, updated!

  7. Thanks for the update on this. I went through the original version line-by-line a couple of times and couldn’t figure out why I couldn’t set statuses with spaces / dashes / underscores in them. The pages just completely refused to match the styles and array values if they contained anything other than alphanumeric characters. Ideas?

    1. Gabriel Merovingi July 21, 2011 at 11:35 pm

      Hey Drew. 

      I just tried this code and put in some values like “Special Offer”, “Used/Old” and “20% off!” and it works like a charm. Not sure what could be the issue if your using the code above just at it is.

      1. Hmm, weird. I’ll give it another go. How did you match the style names when the array values had spaces and or symbols?

        1. Gabriel Merovingi July 23, 2011 at 11:46 pm

          The examples I gave above resulted of course in the style defaulting to the custom_state (gray box) but that can probably be adjusted with cleaning up the names you use or by giving them a custom value which you will have to match in the custom_post_state function.

          Personally I always go for keeping things simple. If I would use this function with special character statuses I would assign some simpler css style names. 

          An example of this would be:

          if ( $show_custom_state[0] == ‘20% Off!’ ) $style = ‘orange’;
          elseif ($show_custom_state[0] == ‘Used/Old’ ) $style = ‘red’;
          etc.

        2. Durr, dunno why I didn’t think of just calling the array values separately. Thanks!

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!