teamadesign

v3

May 14th, 2014
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. add_action( 'add_meta_boxes', 'wpse_143600_add_box' );
  2. add_action( 'save_post', 'wpse_143600_save_box' );
  3.  
  4. //create the metabox
  5. function wpse_143600_add_box() {
  6.     add_meta_box(
  7.         'related_testimonial',
  8.         __( 'community', 'wpse_143600_translation' ),
  9.         'wpse_143600_testimonial_box',
  10.         'testimonial',
  11.   'normal'
  12.     );
  13. }
  14.  
  15. //build the box
  16. function wpse_143600_testimonial_box($post) {
  17.  
  18.     wp_nonce_field( basename( __FILE__ ), 'wpse_143600_nonce' );
  19.  
  20.     $wpse_143600_stored_meta = get_post_meta( $post->ID );
  21.    
  22.     $testimonialArgs = array(
  23.   'post_type' => 'community',
  24.   'post_status' => 'publish',
  25.   'numberposts' => -1
  26.     );
  27.    
  28.     $testimonials = get_posts($testimonialArgs);
  29.    
  30.     if($testimonials): ?>
  31.     <p>
  32.       <label for="meta-select" class="wpse_143600-row-title"><?php _e( 'Example Select Input', 'wpse_143600_translation' )?></label>
  33.       <select name="meta-select" id="meta-select">
  34.         <option value="NULL">Please choose a testimonial…</option>
  35.     <?php foreach($testimonials as $testimonial): ?>
  36.         <option value="<?php echo $testimonial->ID; ?>" <?php if ( isset ( $wpse_143600_stored_meta['meta-select'] ) ) selected( $wpse_143600_stored_meta['meta-select'][0], $testimonial->ID ); ?>><?php echo $testimonial->post_title; ?></option>
  37.     <?php endforeach; ?>
  38.       </select>
  39.     </p>    
  40.     <?php
  41.     else:
  42.     ?>
  43.   <p>There are no testimonials - please save this post, and write some testimonials. You'll then be able to choose a testimonial for this location.</p>
  44.     <?php
  45.     endif;
  46. }
  47.  
  48. //save the box
  49. function wpse_143600_save_box( $post_id ) {
  50.     $is_autosave = wp_is_post_autosave( $post_id );
  51.     $is_revision = wp_is_post_revision( $post_id );
  52.     $is_valid_nonce = ( isset( $_POST[ 'wpse_143600_nonce' ] ) && wp_verify_nonce( $_POST[ 'wpse_143600_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
  53.  
  54.     // Exits script depending on save status
  55.     if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
  56.         return;
  57.     }
  58.     // Checks for input and saves if needed
  59.     if( isset( $_POST[ 'meta-select' ] ) ) {
  60.       update_post_meta( $post_id, 'meta-select', $_POST[ 'meta-select' ] );
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment