[wp-hackers] Custom post publish: show error $notice in stead of update $message

Barry Ceelen barry at multipop.org
Wed Nov 17 10:23:56 UTC 2010


Thanks a lot for pointing out the admin_notices action, Andrew :)

Problem solved:

add_action( 'admin_notices', 'my_custom_post_type_admin_notices' );

function my_custom_post_type_admin_notices() {

	global $pagenow;

	if ( 'post.php' != $pagenow ) // Only show notice, if any, on the post edit screen
		return;

	if ( $post->post_type == 'my_custom_post_type' && isset( $_GET['message'] ) && $_GET['message'] == 'some_variable' )
		echo '<div class="error"><p><strong>Note: </strong> please provide some missing info</p></div>';
}





On 16 Nov, 2010, at 18:57 , Andrew Nacin wrote:

> On Tue, Nov 16, 2010 at 11:51 AM, Barry Ceelen <barry at multipop.org> wrote:
> 
>> Hi all,
>> 
>> I'm using a validation function which fires when publishing a particular
>> custom post type:
>> 
>> add_action( 'publish_my_custom_post_type', 'validate_my_custom_post_type'
>> );
>> 
>> function validate_my_custom_post_type() {
>>       global $post;
>>       if ( $some_form_value == '' ) {
>>               $sendback = admin_url(
>> 'post.php?post='.$post->ID.'&action=edit&message=200' );
>>               wp_redirect( $sendback );
>>               exit;
>>       }
>> }
>> 
>> 
>> Although I'm able to filter the feedback messages to show a message when
>> the condition is not met, prompting the user to take action..
>> 
>> add_filter( 'post_updated_messages', 'my_custom_post_type_feedback' );
>> 
>> function my_custom_post_type_feedback( $messages ) {
>>       $messages['my_custom_post_type']['200'] = __( 'Some feedback
>> message.', 'my_i18n_identifier' );
>>       return $messages;
>> }
>> 
>> ..I'm looking to show a $notice (error message, different color) in stead
>> if a $message, but haven't found a way to do so.
>> Any pointers on how to accomplish this?
>> 
> 
> I wouldn't overload the post_updated_messages filter. That filter is
> designed to alter the existing post/page messages. If you want your own
> message, then redirect with a query var you can grab, and use the
> admin_notices action to drop in a notice.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list