daily pastebin goal
48%
SHARE
TWEET

Untitled

a guest Dec 9th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?
  2. $prefix = 'fds_';
  3.  
  4. $meta_box = array(
  5.     'id' => 'thumb-box',
  6.     'title' => '首頁縮圖設定',
  7.     'page' => 'post',
  8.     'context' => 'normal',
  9.     'priority' => 'high',
  10.     'fields' => array(
  11.         array(
  12.             'name' => '首頁縮圖寬度',
  13.             'desc' => '填入數值,不需單位',
  14.             'id' => $prefix . 'thumb_width',
  15.             'type' => 'text',
  16.             'std' => '200'
  17.         ),
  18.         array(
  19.             'name' => '首頁縮圖高度',
  20.             'desc' => '填入數值,不需單位',
  21.             'id' => $prefix . 'thumb_height',
  22.             'type' => 'text',
  23.             'std' => '150'
  24.         )
  25.     )
  26. );
  27. add_action('admin_menu', 'mytheme_add_box');
  28.  
  29. // Add meta box
  30. function mytheme_add_box() {
  31.     global $meta_box;
  32.  
  33.     add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
  34. }
  35. // Callback function to show fields in meta box
  36. function mytheme_show_box() {
  37.     global $meta_box, $post;
  38.  
  39.     // Use nonce for verification
  40.     echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  41.  
  42.     echo '<table class="form-table">';
  43.  
  44.     foreach ($meta_box['fields'] as $field) {
  45.         // get current post meta data
  46.         $meta = get_post_meta($post->ID, $field['id'], true);
  47.  
  48.         echo '<tr>',
  49.                 '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  50.                 '<td>';
  51.         switch ($field['type']) {
  52.             case 'text':
  53.                 echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
  54.                 break;
  55.             case 'textarea':
  56.                 echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
  57.                 break;
  58.             case 'select':
  59.                 echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  60.                 foreach ($field['options'] as $option) {
  61.                     echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  62.                 }
  63.                 echo '</select>';
  64.                 break;
  65.             case 'radio':
  66.                 foreach ($field['options'] as $option) {
  67.                     echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  68.                 }
  69.                 break;
  70.             case 'checkbox':
  71.                 echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  72.                 break;
  73.         }
  74.         echo     '<td>',
  75.             '</tr>';
  76.     }
  77.  
  78.     echo '</table>';
  79. }
  80. add_action('save_post', 'mytheme_save_data');
  81.  
  82. // Save data from meta box
  83. function mytheme_save_data($post_id) {
  84.     global $meta_box;
  85.  
  86.     // verify nonce
  87.     if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
  88.         return $post_id;
  89.     }
  90.  
  91.     // check autosave
  92.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  93.         return $post_id;
  94.     }
  95.  
  96.     // check permissions
  97.     if ('page' == $_POST['post_type']) {
  98.         if (!current_user_can('edit_page', $post_id)) {
  99.             return $post_id;
  100.         }
  101.     } elseif (!current_user_can('edit_post', $post_id)) {
  102.         return $post_id;
  103.     }
  104.  
  105.     foreach ($meta_box['fields'] as $field) {
  106.         $old = get_post_meta($post_id, $field['id'], true);
  107.         $new = $_POST[$field['id']];
  108.  
  109.         if ($new && $new != $old) {
  110.             update_post_meta($post_id, $field['id'], $new);
  111.         } elseif ('' == $new && $old) {
  112.             delete_post_meta($post_id, $field['id'], $old);
  113.         }
  114.     }
  115. }
  116. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  117.  return $post_id;
  118. }
  119. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top