How to Create Custom Post Status Messages in the WordPress Admin Panel

Home » Snippets » How to Create Custom Post Status Messages in the WordPress Admin Panel
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

When working with WordPress custom post types, you may find yourself needing to add custom status messages to the admin area. These messages can be helpful for communicating specific information to users about the status of a post, such as when it has been approved or is awaiting review. In this article, we will show you how to create custom post status messages in the WordPress admin area. This can be done with just a few lines of code and will make a big difference in how users interact with your custom post types.

					function wpturbo_custom_post_status_messages( $messages ) {
    global $post, $post_ID;

    $messages['custom_status'] = array(
        0 => '', // Unused. Messages start at index 1.
        1 => __( 'Custom status updated.', 'wpturbo' ),
        2 => __( 'Custom status updated.', 'wpturbo' ),
        3 => __( 'Custom status deleted.', 'wpturbo' ),
        4 => __( 'Custom status updated.', 'wpturbo' ),
        5 => isset( $_GET['revision'] ) ? sprintf( __( 'Custom status restored to revision from %s', 'wpturbo' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6 => __( 'Custom status published.', 'wpturbo' ),
        7 => __( 'Custom status saved.', 'wpturbo' ),
    );

    return $messages;
}
add_filter( 'post_updated_messages', 'wpturbo_custom_post_status_messages' );
				

The code snippet above allows you to create custom post status messages in your WordPress admin dashboard for a custom post status that you have created.

The function wpturbo_custom_post_status_messages(), which is added to the post_updated_messages filter, enables you to customize messages that are displayed to the user based on certain actions. By default, WordPress shows standard messages for actions such as publishing, trashing, or revising posts, but with this function, you can create custom message for any custom post status that you create.

First, the global $post and $post_ID variables are set to ensure that they can be used in the function.

Next, we define an array of messages that correspond to each status update that can be performed on the custom status. We use the __() function to make the messages available for translation.

Below is an explanation of each index of the $messages array:

  • Index 0 is unused.
  • Index 1 is the message that gets displayed after the custom status is created or updated.
  • Index 2 is the message that gets displayed if the updated status is an exact copy of the previous, or if it was updated but nothing actually changed.
  • Index 3 is the message that gets displayed if the custom status is deleted.
  • Index 4 is the message displayed if any changes were made during an update to the custom status.
  • Index 5 is the message displayed if reverting to a previous revision of the custom status.
  • Index 6 is the message displayed if the custom status is published.
  • Index 7 is the message displayed if the custom status is saved (but not published).

Once the $messages array is populated with the messages you want to display, it is returned so that WordPress can use it to display the messages accordingly. The add_filter() function is used to add this wpturbo_custom_post_status_messages() function to the post_updated_messages filter.

Register an account to save your snippets or go Pro to get more features.