Advertisement
Guest User

Settings Errors

a guest
Feb 16th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1. <?php
  2.  
  3. add_action( 'add_meta_boxes', 'acme_add_metaboxes' );
  4. function acme_add_metaboxes() {
  5.     add_meta_box( 'acme-metabox-mandatory-field', __( 'Mandatory Field', 'acme' ), 'metabox_mandatory_field', 'acme', 'normal', 'high' );
  6. }
  7.  
  8. function metabox_mandatory_field( $post ) {
  9.     settings_error( 'mandatory-field' );
  10.  
  11.     echo '<input type="text" name="mandatory-field" value="' . get_post_meta( $post->ID, 'mandatory_field', true ) . '">';
  12. }
  13.  
  14. add_action( 'save_post', 'acme_save' );
  15. function acme_save( $post ) {
  16.  
  17.     // Classic veririfications here
  18.    
  19.     // Check for mandatory field
  20.     if ( isset( $_POST['mandatory-field'] ) ) {
  21.         if ( ! empty( $_POST['mandatory-field'] ) ) {
  22.             update_post_meta( $post->ID, 'mandatory_field', sanitize_text_field( $_POST['mandatory-field'] ) );
  23.         } else {
  24.             add_settings_error( 'mandatory-field', '', __( 'You must define a value in the metabox.', 'acme' ), 'error' );
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement