[wp-hackers] CPT meta box error messages

Ken (WraithKenny) ken.adcstudio at gmail.com
Thu Oct 27 15:08:49 UTC 2011


There's a add_settings_error function that's part of the Settings API
http://codex.wordpress.org/Function_Reference/add_settings_error I
haven't looked into it much, but could be worth checking out.

On Sun, Oct 9, 2011 at 6:13 PM, Simon Blackbourn <piemanek at gmail.com> wrote:
>>
>> 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
> _______________________________________________
> 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