Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action( 'add_meta_boxes', 'acme_add_metaboxes' );
- function acme_add_metaboxes() {
- add_meta_box( 'acme-metabox-mandatory-field', __( 'Mandatory Field', 'acme' ), 'metabox_mandatory_field', 'acme', 'normal', 'high' );
- }
- function metabox_mandatory_field( $post ) {
- settings_error( 'mandatory-field' );
- echo '<input type="text" name="mandatory-field" value="' . get_post_meta( $post->ID, 'mandatory_field', true ) . '">';
- }
- add_action( 'save_post', 'acme_save' );
- function acme_save( $post ) {
- // Classic veririfications here
- // Check for mandatory field
- if ( isset( $_POST['mandatory-field'] ) ) {
- if ( ! empty( $_POST['mandatory-field'] ) ) {
- update_post_meta( $post->ID, 'mandatory_field', sanitize_text_field( $_POST['mandatory-field'] ) );
- } else {
- add_settings_error( 'mandatory-field', '', __( 'You must define a value in the metabox.', 'acme' ), 'error' );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement