wordpress snippets last updated   : July 14, 2014
we now have 634 snippets

Create custom post status mesasges in admin

wordpress snippet

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 below". 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.
snippet :  PHPcopy
	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.
  • Pingback: How to create custom status icons for custom post | WpCode.net

  • http://jaxov.com Jaxov.com

    Great addon ;-)  I was wondering, i tried it on my WP 3.1.3 and it works great. The only thing i wanted to ask is how can i remove the status, after the article has been re-uploaded. I tried using the “Custom” or the “——” but than one of these show in the “Post” list as well. 

    • http://wpsnipp.com Kevin Chard

      The easy way would be to set display:none; on one of the statuses you create, eg: .final{display:none;} it would still create the status just not display it.

      • Gabriel Merovingi

        Thanks for this! I have adjusted your code to delete the status if set to “None”. Also included so only those who can “publish_posts” can change the status while everyone sees the saved results of course.

        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[] = ” . $show_custom_state[0] . ”;
        return $states;
        }
        add_action( ‘admin_head’, ‘status_css’ );
        function status_css() {
        echo ‘

        .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; 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;}
        ‘;
        }
        if ( current_user_can( ‘publish_posts’ ) ) {
        add_action( ‘post_submitbox_misc_actions’, ‘custom_status_metabox’ );
        add_action( ‘save_post’, ‘save_status’ );
        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 ‘Custom status: ‘;

        foreach ( $i = 0; $i < count( $custom_status ); $i++ ) {

        echo '’ . $custom_status[$i] . ”;
        }
        echo ”;
        }
        function save_status( $post_id ) {

        if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE ) return $post_id;

        // If the status is set to “None” we want to delete this setting.

        if ( $_POST["ourstatus"] == ‘None’ ) delete_post_meta( $post_id, “_status”, $_POST["ourstatus"] );

        else update_post_meta( $post_id, “_status”, $_POST["ourstatus"] );

        }
        }

        • http://wpsnipp.com Kevin Chard

          Cool Gabriel nice update, you should contact me via http://wpsnipp.com/contact/So I can post the update attributed to you as post author.

        • http://twitter.com/wajanusdesigns Alexander Janus

          Made a slight modification to the code, yours had syntax errors and you used “foreach” instead of “for”

          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[] = ” . $show_custom_state[0] . ”;
          return $states;
          }
          add_action( ‘admin_head’, ‘status_css’ );
          function status_css() {
          echo ‘

          .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; 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;}
          ‘;
          }
          if ( current_user_can( ‘publish_posts’ ) ) {
          add_action( ‘post_submitbox_misc_actions’, ‘custom_status_metabox’ );
          add_action( ‘save_post’, ‘save_status’ );
          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 ‘Custom status: ‘;
          for ( $i = 0; $i < count( $custom_status ); $i++ ) {
          echo '’ . $custom_status[$i] . ”;
          }

          echo ”;
          }
          function save_status( $post_id ) {

          if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE ) return $post_id;

          // If the status is set to “None” we want to delete this setting.

          if ( $_POST["ourstatus"] == ‘None’ ) delete_post_meta( $post_id, “_status”, $_POST["ourstatus"] );

          else update_post_meta( $post_id, “_status”, $_POST["ourstatus"] );

          }
          }

          • hookedonweb_usa

            if ( $_POST["ourstatus"] == ‘None’ ) delete_post_meta( $post_id, “_status”, $_POST["ourstatus"] );

            is throwing an error: Undefined index: ourstatus

            So I did this:

            if ( isset($_POST["ourstatus"]) == ‘None’ ) delete_post_meta( $post_id, “_status”, $_POST["ourstatus"] );

            else update_post_meta( $post_id, “_status”, $_POST["ourstatus"] );

            is throwing the same error: Undefined index: ourstatus

            so did the same:

            if ( isset($_POST["ourstatus"]) == ‘None’ ) delete_post_meta( $post_id, “_status”, isset($_POST["ourstatus"] ));

            But I can not seem to clear this final error:

            Undefined index: ps_right_now in . . . wp-content/plugins/post-status-menu-items/cms_post_status_menu.php

  • Pingback: Wordpress Update: Create custom post status mesasges in admin – 400+ Wordpress code snippets for your blog

  • Pingback: 29 Wordpress Tweaks to Improve Posts and Pages

  • http://www.cliffpaulick.com Cliff Paulick

    below, not bellow. :)

    • http://wpsnipp.com Kevin Chard

      WHooops :) ty

  • http://xaxvi.com Bakar Lomsadze

    hi, is there any chance to hide this feature from non administrators in wordpress?

    • http://wpsnipp.com Kevin Chard

       If you want to remove all aspects from regular users you could just do something like this with the hooks,

      if(is_admin()){
      add_action( ‘post_submitbox_misc_actions’, ‘custom_status_metabox’ );
      add_filter( ‘display_post_states’,’custom_post_state’);
      }

      If you want to display the status but not give non admin users access to change it you can do the same but just with the function that displays the interface options.

      if(is_admin()){
      add_action( ‘post_submitbox_misc_actions’, ‘custom_status_metabox’ );
      }

      I have not tested this but should work,

  • Edson Santoro

    GREAT!!!!!!!!!!!!! Saved my life!

jqsnipp.com | we just launched jquery snippets website