Guest User

CPT Meta Boxes

a guest
Oct 2nd, 2013
1,010
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. add_action( 'add_meta_boxes', 'cd_meta_box_add' );
  2. function cd_meta_box_add()
  3. {
  4.     add_meta_box( 'mtb-attorney', 'Attorney Info', 'cd_meta_box_cb', 'attorney', 'normal', 'high' );
  5. }
  6.  
  7. function cd_meta_box_cb( $post )
  8. {
  9.     $values = get_post_custom( $post->ID );
  10. $text00 = isset( $values['att_meta_title'] ) ? esc_attr( $values['att_meta_title'][0] ) : '';
  11.     $text0 = isset( $values['att_meta_phone'] ) ? esc_attr( $values['att_meta_phone'][0] ) : '';
  12. $text1 = isset( $values['att_meta_email'] ) ? esc_attr( $values['att_meta_email'][0] ) : '';
  13. $text2 = isset( $values['att_meta_bio'] ) ? esc_attr( $values['att_meta_bio'][0] ) : '';
  14. $text3 = isset( $values['att_meta_edu'] ) ? esc_attr( $values['att_meta_edu'][0] ) : '';
  15.     wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
  16.     ?>
  17. <h3>Complete the below contact, biography, and education fields for each attorney in your firm.  When complete, press the Save Draft button and then press Add New Attorney to add more.</h3>
  18. <p>
  19.         <label class="mtlabel" for="att_meta_title">Name</label>
  20.         <input type="text" class="mtinput" name="post_title" id="att_meta_title" value="<?php echo the_title(); ?>" />
  21.     </p>
  22.     <p>
  23.         <label class="mtlabel" for="att_meta_phone">Phone Number</label>
  24.         <input type="text" class="mtinput" name="att_meta_phone" id="att_meta_phone" value="<?php echo $text0; ?>" />
  25.     </p>
  26.     <p>
  27.         <label class="mtlabel" for="att_meta_email">E Mail</label>
  28.         <input type="text" class="mtinput" name="att_meta_email" id="att_meta_email" value="<?php echo $text1; ?>" />
  29.     </p>
  30. <p>
  31.         <label class="mtlabel" for="att_meta_bio">Biography</label>
  32.         <textarea class="mtinput theEditor" name="att_meta_bio" id="att_meta_bio" rows="4" cols="50"><?php echo $text2; ?></textarea>
  33.     </p>
  34. <p>
  35.         <label class="mtlabel" for="att_meta_edu">Education</label>
  36.         <input type="text" class="mtinput" name="att_meta_edu" id="att_meta_edu" value="<?php echo $text3; ?>" />
  37.     </p>
  38.     <?php  
  39. }
  40.  
  41.  
  42. add_action( 'save_post', 'cd_meta_box_save' );
  43. function cd_meta_box_save( $post_id )
  44. {
  45.     // Bail if we're doing an auto save
  46.     if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
  47.    
  48.     // if our nonce isn't there, or we can't verify it, bail
  49.     if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
  50.    
  51.     // if our current user can't edit this post, bail
  52.     if( !current_user_can( 'edit_post', $post_id ) ) return;
  53.    
  54.     // now we can actually save the data
  55.     $allowed = array(
  56.         'a' => array( // on allow a tags
  57.             'href' => array() // and those anchords can only have href attribute
  58.         )
  59.     );
  60.    
  61.     // Probably a good idea to make sure your data is set
  62. if( isset( $_POST['att_meta_title'] ) )
  63.         update_post_meta( $post_id, 'att_meta_title', wp_kses( $_POST['att_meta_title'], $allowed ) );
  64.  
  65.     if( isset( $_POST['att_meta_phone'] ) )
  66.         update_post_meta( $post_id, 'att_meta_phone', wp_kses( $_POST['att_meta_phone'], $allowed ) );
  67.  
  68. if( isset( $_POST['att_meta_email'] ) )
  69.         update_post_meta( $post_id, 'att_meta_email', wp_kses( $_POST['att_meta_email'], $allowed ) );
  70. if( isset( $_POST['att_meta_bio'] ) )
  71.         update_post_meta( $post_id, 'att_meta_bio', wp_kses( $_POST['att_meta_bio'], $allowed ) );
  72.         if( isset( $_POST['att_meta_edu'] ) )
  73.         update_post_meta( $post_id, 'att_meta_edu', wp_kses( $_POST['att_meta_edu'], $allowed ) );
  74. }
  75.  
  76. // Post Type Practice Area
  77. add_action( 'add_meta_boxes', 'cd_meta_box_add2' );
  78. function cd_meta_box_add2()
  79. {
  80.     add_meta_box( 'mtb-pta', 'Your Practice Areas', 'cd_meta_box_cb2', 'practice-area', 'normal', 'high' );
  81. }
  82.  
  83. function cd_meta_box_cb2( $post )
  84. {
  85.     $values = get_post_custom( $post->ID );
  86.     $text = isset( $values['pa_meta_h1'] ) ? esc_attr( $values['pa_meta_h1'][0] ) : '';
  87. $text1 = isset( $values['pa_meta_desc'] ) ? esc_attr( $values['pa_meta_desc'][0] ) : '';
  88. $text2 = isset( $values['pa_meta_h2'] ) ? esc_attr( $values['pa_meta_h2'][0] ) : '';
  89. $text3 = isset( $values['pa_meta_desc2'] ) ? esc_attr( $values['pa_meta_desc2'][0] ) : '';
  90.     wp_nonce_field( 'my_meta_box2_nonce', 'meta_box_nonce' );
  91.     ?>
  92. <h3>Complete the below fields to add information about your areas of practice  To add more, save draft and then press the Add New Practice Area button.</h3>
  93.     <p>
  94.         <label class="mtlabel" for="pa_meta_h1">Practice Area Heading 1</label>
  95.         <input type="text" class="mtinput" name="pa_meta_h1" id="pa_meta_h1" value="<?php echo $text; ?>" />
  96.     </p>
  97.     <p>
  98.         <label class="mtlabel" for="pa_meta_desc">Practice Area Description</label>
  99.         <textarea class="mtinput" rows="10" cols="125" name="pa_meta_desc" id="pa_meta_desc"><?php echo $text1; ?></textarea>
  100.     </p>
  101. <p>
  102.         <label class="mtlabel" for="pa_meta_h2">Practice Area Heading 2</label>
  103.         <input type="text" class="mtinput" name="pa_meta_h2" id="pa_meta_h2" value="<?php echo $text2; ?>" />
  104.     </p>
  105. <p>
  106.         <label class="mtlabel" for="pa_meta_desc2">Practice Area Description</label>
  107.         <textarea class="mtinput" rows="10" cols="125" name="pa_meta_desc2" id="pa_meta_desc2"><?php echo $text3; ?></textarea>
  108.     </p>
  109.  
  110.     <?php  
  111. }
  112.  
  113.  
  114. add_action( 'save_post', 'cd_meta_box_save2' );
  115. function cd_meta_box_save2( $post_id )
  116. {
  117.     // Bail if we're doing an auto save
  118.     if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
  119.    
  120.     // if our nonce isn't there, or we can't verify it, bail
  121.     if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box2_nonce' ) ) return;
  122.    
  123.     // if our current user can't edit this post, bail
  124.     if( !current_user_can( 'edit_post', $post_id ) ) return;
  125.    
  126.     // now we can actually save the data
  127.     $allowed = array(
  128.         'a' => array( // on allow a tags
  129.             'href' => array() // and those anchords can only have href attribute
  130.         )
  131.     );
  132.    
  133.     // Probably a good idea to make sure your data is set
  134.     if( isset( $_POST['pa_meta_h1'] ) )
  135.         update_post_meta( $post_id, 'pa_meta_h1', wp_kses( $_POST['pa_meta_h1'], $allowed ) );
  136.  
  137. if( isset( $_POST['pa_meta_desc'] ) )
  138.         update_post_meta( $post_id, 'pa_meta_desc', wp_kses( $_POST['pa_meta_desc'], $allowed ) );
  139.  
  140. if( isset( $_POST['pa_meta_h2'] ) )
  141.         update_post_meta( $post_id, 'pa_meta_h2', wp_kses( $_POST['pa_meta_h2'], $allowed ) );
  142.  
  143.         if( isset( $_POST['pa_meta_desc2'] ) )
  144.         update_post_meta( $post_id, 'pa_meta_desc2', wp_kses( $_POST['pa_meta_desc2'], $allowed ) );
  145. }
  146.  
  147. // Post Type About
  148. add_action( 'add_meta_boxes', 'cd_meta_box_add3' );
  149. function cd_meta_box_add3()
  150. {
  151.     add_meta_box( 'mtb-about', 'Add Info', 'cd_meta_box_cb3', 'about', 'normal', 'high' );
  152. }
  153.  
  154. function cd_meta_box_cb3( $post )
  155. {
  156.     $values = get_post_custom( $post->ID );
  157.     $text = isset( $values['bout_meta_rm'] ) ? esc_attr( $values['bout_meta_rm'][0] ) : '';
  158. $text1 = isset( $values['bout_meta_vid'] ) ? esc_attr( $values['bout_meta_vid'][0] ) : '';
  159.     wp_nonce_field( 'my_meta_box3_nonce', 'meta_box_nonce' );
  160.     ?>
  161.  
  162.     <p>
  163.         <label class="mtlabel" for="bout_meta_rm">Recent Matters</label>
  164.         <input type="text" class="mtinput" name="bout_meta_rm" id="bout_meta_rm" value="<?php echo $text; ?>" />
  165.     </p>
  166.     <p>
  167.         <label class="mtlabel" for="bout_meta_vid">Video Embed Code</label>
  168.         <input type="text" class="mtinput" name="bout_meta_vid" id="bout_meta_vid" value="<?php echo $text1; ?>" />
  169.     </p>
  170.  
  171.     <?php  
  172. }
  173.  
  174.  
  175. add_action( 'save_post', 'cd_meta_box_save3' );
  176. function cd_meta_box_save3( $post_id )
  177. {
  178.     // Bail if we're doing an auto save
  179.     if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
  180.    
  181.     // if our nonce isn't there, or we can't verify it, bail
  182.     if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box3_nonce' ) ) return;
  183.    
  184.     // if our current user can't edit this post, bail
  185.     if( !current_user_can( 'edit_post', $post_id ) ) return;
  186.    
  187.     // now we can actually save the data
  188.     $allowed = array(
  189.         'a' => array( // on allow a tags
  190.             'href' => array() // and those anchords can only have href attribute
  191.         )
  192.     );
  193.    
  194.     // Probably a good idea to make sure your data is set
  195.     if( isset( $_POST['bout_meta_rm'] ) )
  196.         update_post_meta( $post_id, 'bout_meta_rm', wp_kses( $_POST['bout_meta_rm'], $allowed ) );
  197.  
  198. if( isset( $_POST['bout_meta_vid'] ) )
  199.         update_post_meta( $post_id, 'bout_meta_vid', wp_kses( $_POST['bout_meta_vid'], $allowed ) );
  200.  
  201. }
RAW Paste Data