Change the WordPress post updated messages

Sometimes it is necessary to modify or remove the default WordPress post updated messages that are displayed when making changes to a Post in WordPress.

One example is when you are creating a custom post type that does not have a permalink. When you save a draft, publish or update a published post, you are presented with messaging that includes a link to the post – which in that case will take the user to a 404 page.

Snippet

The post_updated_messages  filter gives you access to the default WordPress post updated messages for all post types and the $messages parameter is  an associative array with with each index being the name of the post type and the corresponding values as an array.

add_filter( 'post_updated_messages', 'rw_post_updated_messages' );

/**
 * My Post Type update messages.
 *
 * See /wp-admin/edit-form-advanced.php
 *
 * @param array $messages Existing post update messages.
 *
 * @return array Amended post update messages with new CPT update messages.
 */

function rw_post_updated_messages( $messages ) {

	$post             = get_post();
	$post_type        = get_post_type( $post );
	$post_type_object = get_post_type_object( $post_type );
	
	$messages['my-post-type'] = array(
		0  => '', // Unused. Messages start at index 1.
		1  => __( 'My Post Type updated.' ),
		2  => __( 'Custom field updated.' ),
		3  => __( 'Custom field deleted.'),
		4  => __( 'My Post Type updated.' ),
		/* translators: %s: date and time of the revision */
		5  => isset( $_GET['revision'] ) ? sprintf( __( 'My Post Type restored to revision from %s' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
		6  => __( 'My Post Type published.' ),
		7  => __( 'My Post Type saved.' ),
		8  => __( 'My Post Type submitted.' ),
		9  => sprintf(
			__( 'My Post Type scheduled for: <strong>%1$s</strong>.' ),
			// translators: Publish box date format, see http://php.net/date
			date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) )
		),
		10 => __( 'My Post Type draft updated.' )
	);

        //you can also access items this way
        // $messages['post'][1] = "I just totally changed the Updated messages for standards posts";

        //return the new messaging 
	return $messages;
}

Checkout the Codex entry for register_post_type() for more details on registering post types in general.

Hope it helps!

 

 

This Post Has One Comment

Comments are closed.