I have a question about how to stop processing a post when a plugin(server side) finds errors on custom fields. Basically I developed a plugin which adds and process some custom fields in the edit post panel. When i click on publish/update button i'd like to stop the process of updating or saving the post...in fact the plugin can stop the process of storing data from the custom fields when occurres errors but even with errors, posts can be published or updated anyway. How can I handle this situation? Thank you all!

1 Answer 1


welcome to WordPress Answers. I'm afraid there isn't a (server-side) way of preventing posts being submitted.

The hook that is fired when a post is saved, save_post, is fired after the post and its custom fields are saved to the database.

However, you can always retrieve the custom field values, validate them and then delete any invalid fields

(Keep in mind that you'll only want to delete custom fields with keys that your plug-in deals with. Users will not be impressed if they can't save their own custom field data because your plug-in thinks its invalid. On a related note, you should prefix your custom field keys with a prefix unique to your plugin)

Anyway, do to the above... (note: this is untested)

add_action('save_post', 'my_save_post');
function my_save_post($post_id) {

    //Make sure you check this isn't an autosave.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )

    // Check if the user can edit this post
     if ( !current_user_can( 'edit_post', $post->ID ) )

    //The keys you wish to validate. For best practise, keys should be prefixed.
    $plugin_keys = array('my_plugin_some_key','my_plugin_another_key','my_plugin_don_key');

   foreach($plugin_keys as $key):
        $values = get_post_custom_values($key, $post_id);
        //validate value
        //Rather than deleting invalid data, you could use update_post_meta
        $is_invalid = false; //set to true if the value is invalid.
            delete_post_meta($post_id, $key);//delete invalid data  

A few words of warning. $values is an array of values associated with key $key. It could be an array of 1, or any number. This is something you'll need to keep in mind when validating the values.

Secondly, delete_post_meta($post_id, $key) delete's all of the posts custom field values with key $key. If you only wish to delete a value-key pair, you'll want delete_post_meta($post_id, $key,$old_value).

For more information on these see the Codex on delete_post_meta and get_post_custom_values.

Finally if you wish to change the post status back to draft if you find invalid data, you may wish to look at my answer to this question.

  • Thank you Stephen. At the moment my plugin already does the validation on the custom fields in the way that you mentioned, and if one of them is invalid it doesn't process the metadata. Anyway I didn't think about the post status...it's a good idea to reset the post to draft if one of the fields is invalid. Thank you so much I'll give it a try.
    – Tony
    Feb 13, 2012 at 16:35
  • No problem! So you're saving data from a custom metabox? (Custom fields in the custom field metabox get handled automatically - that is they are saved prior to save_posts hook). Don't forget to mark answers as correct so others know you're question has been answered / what the solution was :D. Feb 13, 2012 at 16:43
  • Sorry the silly question, but how can I mark the answers?
    – Tony
    Feb 13, 2012 at 16:53
  • Click the grey tick to the left of the question :D. Feb 13, 2012 at 16:55
  • done....eheheh sorry about that!I'm a newbe in here!!
    – Tony
    Feb 13, 2012 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.