Advertisement
Guest User

Untitled

a guest
May 23rd, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.29 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.     // Add the Meta Box
  5.     function add_custom_meta_box() {
  6.         add_meta_box(
  7.             'custom_meta_box', // $id
  8.             'Custom Meta Box', // $title
  9.             'show_custom_meta_box', // $callback
  10.             'products', // $page
  11.             'normal', // $context
  12.             'high'); // $priority
  13.     }
  14.     add_action('add_meta_boxes', 'add_custom_meta_box');
  15.  
  16.  
  17.  
  18.  
  19.       // Field Array
  20.       $prefix = 'custom_';
  21.       $custom_meta_fields = array(
  22.         array(
  23.             'label' => 'Category',
  24.             'id'    => 'category',
  25.             'type'  => 'tax_checkbox'
  26.         ) );
  27.  
  28.  
  29.  
  30.       // The Callback
  31.       function show_custom_meta_box() {
  32.       global $custom_meta_fields, $post;
  33.       // Use nonce for verification
  34.       echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
  35.  
  36.         // Begin the field table and loop
  37.         echo '<table class="form-table">';
  38.         foreach ($custom_meta_fields as $field) {
  39.             // get value of this field if it exists for this post
  40.             $meta = get_post_meta($post->ID, $field['id'], true);
  41.             // begin a table row with
  42.             echo '<tr>
  43.                    <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
  44.                    <td>';
  45.                     switch($field['type']) {
  46.  
  47.  
  48.  
  49. case 'tax_checkbox':
  50.  
  51.  
  52. $terms = get_terms($field['id'],'get=all' );
  53. $post_terms = wp_get_object_terms( get_the_ID(), $field['id'] );
  54.  
  55. $taxonomy = get_taxonomy( $field['id'] );
  56.  
  57. $checked = $post_terms ? $taxonomy->hierarchical ? $post_terms[0]->term_id : $post_terms[0]->slug : null;
  58.  
  59. foreach ( $terms as $term ) {
  60.                                 $term_value = $taxonomy->hierarchical ? $term->term_id : $term->slug;
  61.                                 echo '<input type="checkbox" value="' . $term_value . '" name="' . $field['id'] . '[]" id="term-' . $term_value . '"' . checked( $checked, $term_value, false ) . ' /> <label for="term-' . $term_value . '">' . $term->name . '</label><br />';
  62.                             }
  63.                             echo '<span class="description">' . $field['desc'] . ' <a href="'.get_bloginfo( 'url' ) . '/wp-admin/edit-tags.php?taxonomy=' . $field['id'] . '&post_type=' . $page . '">Manage ' . $taxonomy->label . '</a></span>';
  64.                         break;
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                     } //end switch
  71.             echo '</td></tr>';
  72.         } // end foreach
  73.         echo '</table>'; // end table
  74.       }
  75.  
  76.  
  77.  
  78.     // Save the Data
  79.     function save_custom_meta($post_id) {
  80.         global $custom_meta_fields;
  81.  
  82.         // verify nonce
  83.         if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
  84.             return $post_id;
  85.         // check autosave
  86.         if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
  87.             return $post_id;
  88.         // check permissions
  89.         if ('page' == $_POST['post_type']) {
  90.             if (!current_user_can('edit_page', $post_id))
  91.                 return $post_id;
  92.             } elseif (!current_user_can('edit_post', $post_id)) {
  93.                 return $post_id;
  94.         }
  95.  
  96.         // loop through fields and save the data
  97.         foreach ($custom_meta_fields as $field) {
  98.  
  99.  
  100.         if($field['type'] == 'tax_select') continue;  
  101.  
  102.  
  103.         if( in_array( $field['type'], array( 'tax_checkboxes' ) ) ) {
  104.                         // save taxonomies
  105.                         if ( isset( $_POST[$field['id']] ) ) {
  106.                             $term = $_POST[$field['id']];
  107.                             wp_set_object_terms( $post_id, $term, $field['id'] );
  108.                         }
  109.                     }
  110.  
  111.                     else {
  112.  
  113.  
  114.             $old = get_post_meta($post_id, $field['id'], true);
  115.             $new = $_POST[$field['id']];
  116.             if ($new && $new != $old) {
  117.                 update_post_meta($post_id, $field['id'], $new);
  118.             } elseif ('' == $new && $old) {
  119.                 delete_post_meta($post_id, $field['id'], $old);
  120.             }
  121.         } } // end foreach
  122.  
  123.         // save taxonomies
  124.         $post = get_post($post_id);
  125.         $category = $_POST['category'];
  126.         wp_set_object_terms( $post_id, $category, 'category' );
  127.     }
  128.     add_action('save_post', 'save_custom_meta');  
  129.  
  130.  
  131.  
  132.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement