Advertisement
Guest User

Untitled

a guest
May 1st, 2011
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.99 KB | None | 0 0
  1. <?php
  2. /**
  3. * This file handles the custom meta boxes found on posts and pages.
  4. */
  5.  
  6. // post fields
  7. $post_fields = array(
  8.     array(
  9.         'name' => 'Sub Title',
  10.         'desc' => 'Give this post a sub title?',
  11.         'id' => 'subtitle',
  12.         'type' => 'text',
  13.         'std' => ''
  14.     ),
  15.     array(
  16.         'name' => 'Additional Details',
  17.         'desc' => 'Enter extra post details here',
  18.         'id' => 'add_details',
  19.         'type' => 'textarea',
  20.         'std' => ''
  21.     ),
  22.     array(
  23.         'name' => 'Choose a Fruit',
  24.         'id' => 'fruit',
  25.         'type' => 'select',
  26.         'options' => array('Apples', 'Oranges', 'Pears', 'Pineapples')
  27.     ),
  28.     array(
  29.         'name' => 'Check 1',
  30.         'id' => 'check1',
  31.         'type' => 'checkbox',
  32.     ),
  33.     array(
  34.         'name' => 'Check 2',
  35.         'id' => 'check2',
  36.         'type' => 'checkbox',
  37.     ),
  38.     array(
  39.         'name' => 'Check 3',
  40.         'id' => 'check3',
  41.         'type' => 'checkbox',
  42.     ),
  43. );
  44.  
  45. // page fields
  46. $page_fields = array(
  47.     array(
  48.         'name' => 'Sub Title',
  49.         'desc' => 'Give this post a sub title?',
  50.         'id' => 'subtitle',
  51.         'type' => 'text',
  52.         'std' => ''
  53.     ),
  54. /*  array(
  55.         'name' => 'Additional Details',
  56.         'desc' => 'Enter extra post details here',
  57.         'id' => 'add_details',
  58.         'type' => 'textarea',
  59.         'std' => ''
  60.     ),
  61.     array(
  62.         'name' => 'Choose a Fruit',
  63.         'id' => 'fruit',
  64.         'type' => 'select',
  65.         'options' => array('Apples', 'Oranges', 'Pears', 'Pineapples')
  66.     ),
  67.     array(
  68.         'name' => 'Check 1',
  69.         'id' => 'check1',
  70.         'type' => 'checkbox',
  71.     ),
  72.     array(
  73.         'name' => 'Check 2',
  74.         'id' => 'check2',
  75.         'type' => 'checkbox',
  76.     ),
  77.     array(
  78.         'name' => 'Check 3',
  79.         'id' => 'check3',
  80.         'type' => 'checkbox',
  81.     ),
  82. */
  83. );
  84.  
  85. // Add meta box to editor
  86. function rsgwd_meta_add_box() {
  87.     global $post, $meta_box, $post_fields, $page_fields;
  88.    
  89.     $post_type = $post->post_type;
  90.    
  91.     switch($post_type) {
  92.         case "post":
  93.             $meta_box = array(
  94.                 'id' => 'post_meta_box', // the id of our meta box
  95.                 'title' => 'Post Options', // the title of the meta box
  96.                 'page' => $post_type, // display this meta box on post editing screens
  97.                 'context' => 'normal',
  98.                 'priority' => 'high', // keep it near the top
  99.                 'fields' => $post_fields
  100.             );
  101.             add_meta_box($meta_box['id'], $meta_box['title'], 'display_html', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
  102.             break;
  103.         case "page":
  104.             $meta_box = array(
  105.                 'id' => 'page_meta_box', // the id of our meta box
  106.                 'title' => 'Page Options', // the title of the meta box
  107.                 'page' => $post_type, // display this meta box on post editing screens
  108.                 'context' => 'normal',
  109.                 'priority' => 'high', // keep it near the top
  110.                 'fields' => $page_fields
  111.             );
  112.             add_meta_box($meta_box['id'], $meta_box['title'], 'display_html', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
  113.             break;
  114.         default:
  115.             break;
  116.     }
  117. }
  118. add_action('add_meta_boxes', 'rsgwd_meta_add_box');
  119.  
  120. // Callback function to show fields in meta box
  121. function display_html() {
  122.     global $meta_box, $post; // get the variables from global $meta_box and $post
  123.    
  124.     // Use nonce for verification to check that the person has adequate priveleges
  125.     echo '<input type="hidden" name="rsgwd_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  126.    
  127.     // create the table which the options will be displayed in
  128.     echo '<table class="form-table">';
  129.    
  130.     foreach ($meta_box['fields'] as $field) { // do this for each array inside of the fields array
  131.    
  132.         // get current post meta data
  133.         $meta = get_post_meta($post->ID, $field['id'], true);
  134.        
  135.         echo '<tr>','<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>','<td>';
  136.         switch ($field['type']) {
  137.             case 'text': // the HTML to display for type=text options
  138.                 echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '', $field['desc'];
  139.                 break;
  140.             case 'textarea': // the HTML to display for type=textarea options
  141.                 echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="8" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '', $field['desc'];
  142.                 break;
  143.             case 'select': // the HTML to display for type=select options
  144.                 echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  145.                 foreach ($field['options'] as $option) {echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';}
  146.                 echo '</select>';
  147.                 break;
  148.             case 'radio': // the HTML to display for type=radio options
  149.                 foreach ($field['options'] as $option) {echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];}
  150.                 break;
  151.             case 'checkbox': // the HTML to display for type=checkbox options
  152.                 echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  153.                 break;
  154.         }
  155.         echo '<td>','</tr>';
  156.     }
  157.     echo '</table>';
  158.    
  159.     // echo "<pre>"; var_dump($meta_box['fields']); echo "</pre>";
  160. }
  161.  
  162. // Save data from meta box
  163. function rsgwd_meta_save_data($post_id) {
  164.     global $meta_box;
  165.    
  166.     // echo "<pre>"; var_dump($meta_box); echo "</pre>";
  167.    
  168.     // verify nonce -- checks that the user has access
  169.     if (!wp_verify_nonce($_POST['rsgwd_meta_box_nonce'], basename(__FILE__))) {return $post_id;}
  170.    
  171.     // check autosave
  172.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {return $post_id;}
  173.    
  174.     // check permissions
  175.     if ('page' == $_POST['post_type']) {
  176.         if (!current_user_can('edit_page', $post_id)) {return $post_id;}
  177.     } elseif (!current_user_can('edit_post', $post_id)) {return $post_id;}
  178.    
  179.     foreach ($meta_box['fields'] as $field) { // save each option
  180.         $old = get_post_meta($post_id, $field['id'], true);
  181.         $new = $_POST[$field['id']];
  182.        
  183.         if ($new && $new != $old) { // compare changes to existing values
  184.             update_post_meta($post_id, $field['id'], $new);
  185.         } elseif ('' == $new && $old) {
  186.             delete_post_meta($post_id, $field['id'], $old);
  187.         }
  188.     }
  189. }
  190. add_action('save_post', 'rsgwd_meta_save_data'); // save the data
  191.  
  192. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement