Advertisement
Guest User

functions.php

a guest
Jun 23rd, 2011
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.88 KB | None | 0 0
  1. <?php
  2.  
  3.     //Add meta boxes to post types
  4.     function plib_add_box() {
  5.         global $meta_box;
  6.        
  7.         foreach($meta_box as $post_type => $value) {
  8.             add_meta_box($value['id'], $value['title'], 'plib_format_box', $post_type, $value['context'], $value['priority']);
  9.         }
  10.     }
  11.     //Formatting
  12.     function plib_format_box() {
  13.       global $meta_box, $post;
  14.      
  15.       // verification
  16.       echo '<input type="hidden" name="plib_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  17.      
  18.       echo '<table class="form-table">';
  19.      
  20.       foreach ($meta_box[$post->post_type]['fields'] as $field) {
  21.           // get current post meta data
  22.           $meta = get_post_meta($post->ID, $field['id'], true);
  23.      
  24.           echo '<tr>'.
  25.                   '<th style="width:20%"><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'.
  26.                   '<td>';
  27.           switch ($field['type']) {
  28.               case 'text':
  29.                   echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:97%" />'. '<br />'. $field['desc'];
  30.                   break;
  31.               case 'textarea':
  32.                   echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
  33.                   break;
  34.               case 'select':
  35.                   echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">';
  36.                   foreach ($field['options'] as $option) {
  37.                       echo '<option '. ( $meta == $option ? ' selected="selected"' : '' ) . '>'. $option . '</option>';
  38.                   }
  39.                   echo '</select>';
  40.                   break;
  41.               case 'radio':
  42.                   foreach ($field['options'] as $option) {
  43.                       echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '"' . ( $meta == $option['value'] ? ' checked="checked"' : '' ) . ' />' . $option['name'];
  44.                   }
  45.                   break;
  46.               case 'checkbox':
  47.                   echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ( $meta ? ' checked="checked"' : '' ) . ' />';
  48.                   break;
  49.           }
  50.           echo     '<td>'.'</tr>';
  51.       }
  52.      
  53.       echo '</table>';
  54.      
  55.     }
  56.     // Save data from meta box
  57.     function plib_save_data($post_id) {
  58.         global $meta_box,  $post;
  59.        
  60.         //Verify
  61.         if (!wp_verify_nonce($_POST['plib_meta_box_nonce'], basename(__FILE__))) {
  62.             return $post_id;
  63.         }
  64.      
  65.         //Check > autosave
  66.         if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  67.             return $post_id;
  68.         }
  69.      
  70.         //Check > permissions
  71.         if ('page' == $_POST['post_type']) {
  72.             if (!current_user_can('edit_page', $post_id)) {
  73.                 return $post_id;
  74.             }
  75.         } elseif (!current_user_can('edit_post', $post_id)) {
  76.             return $post_id;
  77.         }
  78.        
  79.         foreach ($meta_box[$post->post_type]['fields'] as $field) {
  80.             $old = get_post_meta($post_id, $field['id'], true);
  81.             $new = $_POST[$field['id']];
  82.            
  83.             if ($new && $new != $old) {
  84.                 update_post_meta($post_id, $field['id'], $new);
  85.             } elseif ('' == $new && $old) {
  86.                 delete_post_meta($post_id, $field['id'], $old);
  87.             }
  88.         }
  89.     }
  90.      
  91.     add_action('save_post', 'plib_save_data');
  92.    
  93.     //We create an array called $meta_box and set the array key to the relevant post type
  94. // If custom post type, change the 'post' variable
  95.     $meta_box['post'] = array(
  96.        
  97.         //This is the id applied to the meta box
  98.         'id' => 'venue_location',  
  99.        
  100.         //This is the title that appears on the meta box container
  101.         'title' => 'Venue/Location',    
  102.        
  103.         //This defines the part of the page where the edit screen section should be shown
  104.         'context' => 'normal',    
  105.        
  106.         //This sets the priority within the context where the boxes should show
  107.         'priority' => 'high',
  108.        
  109.         //Here we define all the fields we want in the meta box
  110.         'fields' => array(
  111.             array(
  112.                 'name' => 'Venue',
  113.                 'desc' => 'Venue Name',
  114.                 'id' => 'venue_info',
  115.                 'type' => 'text',
  116.                 'default' => ''
  117.             ),
  118.             array(
  119.                 'name' => 'Location',
  120.                 'desc' => 'Location of the Venue',
  121.                 'id' => 'location_info',
  122.                 'type' => 'text',
  123.                 'default' => ''
  124.             )
  125.         )
  126.     );
  127.     add_action('admin_menu', 'plib_add_box');
  128. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement