Mandatory (meta) fields in post edit screen
-
With use of custom post types exploding, and WordPress use beyond ‘pages’ and ‘blogging’ being more clearly defined and easier to implement, creating ‘things’ that have compulsory data elements seems a common requirement.
There are quite a few support posts about required or mandatory fields that appear unsatisfactorily answered stretching back over a few years.
How does one make a field compulsory? Is it possible to hook into save_post, check for the field and return a message?
Here is how I thought it might be done:
add_action('save_post', 'my_check_mandatory'); function my_check_mandatory($post_id){ $post = get_post($post_id); if( $post->post_type=='my_post_type' && empty($_POST['mandatory_field']) ){ return new WP_Error('missing_field', __('Mandatory (Exhibitor) field missing')); } }
Any takers on how to actually do this?
- The topic ‘Mandatory (meta) fields in post edit screen’ is closed to new replies.