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

Andrew Nacin wp at andrewnacin.com
Tue Nov 16 17:57:17 UTC 2010


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.


More information about the wp-hackers mailing list