marjwyatt

wsk recipes metaboxes

Nov 19th, 2012
1,219
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $meta_box = array(
  2.     'id' => 'wsk-recipe-details',
  3.     'title' => 'Recipe Details Box',
  4.     'page' => 'recipe',
  5.     'context' => 'normal',
  6.     'priority' => 'high',
  7.     'fields' => array(
  8.         array(
  9.             'name' => 'Recipe Title',
  10.             'desc' => 'Enter Recipe Title',
  11.             'id' => '_recipetitle',
  12.             'type' => 'text',
  13.             'std' => ''
  14.         ),
  15.         array(
  16.             'name' => 'Ingredients',
  17.             'desc' => 'One ingredient per line, outputs as unordered list',
  18.             'id' => '_recipeingred',
  19.             'type' => 'textarea',
  20.             'std' => ''
  21.         ),
  22.         array(
  23.             'name' => 'Instructions',
  24.             'desc' => 'List instructions, one instruction per line, outputs as ordered list',
  25.             'id' => '_recipeinstructions',
  26.             'type' => 'textarea',
  27.             'std' => ''
  28.         ),
  29.         array(
  30.             'name' => 'Variations',
  31.             'desc' => 'List variations, one per line, outputs as unordered list',
  32.             'id' => '_recipevariations',
  33.             'type' => 'textarea',
  34.             'std' => ''
  35.         ),
  36.         array(
  37.             'name' => 'Quick Notes',
  38.             'desc' => 'List any notes ... paragraphs OK',
  39.             'id' => '_recipenotes',
  40.             'type' => 'textarea',
  41.             'std' => ''
  42.         ),
  43.         array(
  44.             'name' => 'Preparation Time',
  45.             'desc' => 'Enter Prep Time',
  46.             'id' => '_recipeprep',
  47.             'type' => 'text',
  48.             'std' => ''
  49.         ),
  50.         array(
  51.             'name' => 'Cooking Time',
  52.             'desc' => 'Enter Cooking Time',
  53.             'id' => '_recipecook',
  54.             'type' => 'text',
  55.             'std' => ''
  56.         ),
  57.         array(
  58.             'name' => 'Servings',
  59.             'desc' => 'Enter Servings Info',
  60.             'id' => '_recipeservings',
  61.             'type' => 'text',
  62.             'std' => ''
  63.         ),
  64.         array(
  65.             'name' => 'Calories',
  66.             'desc' => 'Enter Calories per Serving',
  67.             'id' => '_recipecalories',
  68.             'type' => 'text',
  69.             'std' => ''
  70.         ),
  71.         array(
  72.             'name' => 'Fat',
  73.             'desc' => 'Enter Fat Grams per Serving',
  74.             'id' => '_recipefat',
  75.             'type' => 'text',
  76.             'std' => ''
  77.         ),
  78.         array(
  79.             'name' => 'Protein',
  80.             'desc' => 'Enter Protein Grams per Serving',
  81.             'id' => '_recipeprotein',
  82.             'type' => 'text',
  83.             'std' => ''
  84.         ),
  85.     )
  86. );
  87.  
  88. // Add Recipes Meta Boxes
  89. add_action('admin_menu', 'wsk_add_recipe_meta');
  90. // Add meta box
  91. function wsk_add_recipe_meta() {
  92.     global $meta_box;
  93.     add_meta_box($meta_box['id'], $meta_box['title'], 'wsk_show_recipe_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
  94. }
  95.  
  96. // Callback function to show recipe fields in meta box - NEEDS TO MATCH FIELDS ABOVE
  97. function wsk_show_recipe_box() {
  98.     global $meta_box, $post;
  99.     // Use nonce for verification
  100.     echo '<input type="hidden" name="wsk_recipe_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  101.     echo '<table class="form-table">';
  102.     foreach ($meta_box['fields'] as $field) {
  103.         // get current post meta data
  104.         $meta = get_post_meta($post->ID, $field['id'], true);
  105.         echo '<tr>',
  106.                 '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  107.                 '<td>';
  108.         switch ($field['type']) {
  109.             case 'text':
  110.                 echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
  111.                 break;
  112.             case 'textarea':
  113.                 echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
  114.                 break;
  115.             case 'select': //not currently used but available if needed
  116.                 echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  117.                 foreach ($field['options'] as $option) {
  118.                     echo '<option ', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  119.                 }
  120.                 echo '</select>';
  121.                 break;
  122.             case 'radio': //not currently used but available if needed
  123.                 foreach ($field['options'] as $option) {
  124.                     echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  125.                 }
  126.                 break;
  127.             case 'checkbox': //not currently used but available if needed
  128.                 echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  129.                 break;
  130.         }
  131.         echo     '</td><td>',
  132.             '</td></tr>';
  133.     }
  134.     echo '</table>';
  135. }
  136.  
  137. add_action('save_post', 'wsk_save_recipe_data');
  138. // Save data from meta box
  139. function wsk_save_recipe_data($post_id) {
  140.     global $meta_box;
  141.     // verify nonce
  142.     if (!wp_verify_nonce($_POST['wsk_recipe_meta_box_nonce'], basename(__FILE__))) {
  143.         return $post_id;
  144.     }
  145.     // check autosave
  146.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  147.         return $post_id;
  148.     }
  149.     // check permissions
  150.     if ('page' == $_POST['post_type']) {
  151.         if (!current_user_can('edit_page', $post_id)) {
  152.             return $post_id;
  153.         }
  154.     } elseif (!current_user_can('edit_post', $post_id)) {
  155.         return $post_id;
  156.     }
  157.     foreach ($meta_box['fields'] as $field) {
  158.         $old = get_post_meta($post_id, $field['id'], true);
  159.         $new = $_POST[$field['id']];
  160.         if ($new && $new != $old) {
  161.             update_post_meta($post_id, $field['id'], $new);
  162.         } elseif ('' == $new && $old) {
  163.             delete_post_meta($post_id, $field['id'], $old);
  164.         }
  165.     }
  166. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×