[wp-hackers] CPT meta box error messages

Simon Blackbourn piemanek at gmail.com
Sun Oct 9 22:13:46 UTC 2011


>
> I am trying to figure out how to display a validation error on the input
> (or lack of) from a meta box in a custom post type.
>
>
> I'm fine with the post being saved regardless of the meta inputs.
>
>

If I understand correctly what you are after, you can use the
post_updated_messages filter, so after the post is saved you replace the
usual "Post updated. View post" message with your own custom one:

    add_filter( 'post_updated_messages', 'my_updated_messages' );

    function my_updated_messages( $messages ) {

        global $post, $post_ID;

        if ( 'your_cpt_here' == $post->post_type ) {

            $meta = get_post_meta( $post_ID, 'your_postmeta_name_here', true
);

            if ( '' == $meta ) // or whatever
                $messages[$post->post_type][1] = "Post updated, but you
haven't entered a value for blah. Please enter one and update the post.";
                $messages[$post->post_type][6] = "Post published, but you
haven't entered a value for blah. Please enter one and update the post.";
            }

        }

        return $messages;

    }

You can see the various elements of the $messages array in
edit-form-advanced.php

By removing the "View post" link, it is clear to the author they have to do
something else before they view the post on the site. I also add a class to
the message that adds a little warning sign icon for extra clarity.

Hope that helps
Simon


More information about the wp-hackers mailing list