Advertisement
iamdangavin

Meta Boxes

Jul 6th, 2011
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.95 KB | None | 0 0
  1. <?php function post_save_details(){
  2.   global $post;
  3.  
  4.   if ( ! wp_verify_nonce( $_POST['recipes_subtitle'], 'recipes_subtitle', 'recipes_subtitle' ) )
  5.     return;
  6.  
  7.     $ingredients = array();
  8.     for ( $i = 0, $j = min( count( $_POST['recipe_ingredients']), count( $_POST['recipe_qty'] ) ); $i < $j; $i++ ) {
  9.         if ( empty( $_POST['recipe_ingredients'] ) )
  10.             continue;
  11.         $ingredients[] = array( $_POST['recipe_ingredients'][ $i ], $_POST['recipe_qty'][ $i ] );
  12.     }
  13.     update_post_meta( $post->ID, '_recipe_ingredients', $ingredients );
  14.    
  15.   update_post_meta($post->ID, "sub", $_POST["sub"]);
  16.   update_post_meta($post->ID, "notes", $_POST["notes"]);
  17.   update_post_meta($post->ID, "directions", $_POST["directions"]);
  18.   update_post_meta($post->ID, "serving", $_POST["serving"]);
  19.  
  20. }
  21.  
  22. function recipe_admin_init(){
  23.   add_meta_box("sub-meta", "Sub Title", "recipe_sub", "post", "side", "high");
  24.   add_meta_box("notes_meta", "Notes and Directions", "recipe_notesandmethods_meta", "post", "normal", "high");
  25.   add_meta_box("recipe_meta", "Ingredients And Qty.", "recipe_ingqty_meta", "post", "normal", "high");
  26.   add_action( 'admin_footer-post.php', 'recipe_post_scripts' );
  27.   add_action( 'admin_footer-post-new.php', 'recipe_post_scripts' );
  28. }
  29.  
  30. function recipe_sub(){
  31.   global $post;
  32.   $custom = get_post_custom($post->ID);
  33.   $sub = $custom["sub"][0];
  34.   wp_nonce_field( 'recipes_subtitle', 'recipes_subtitle' );
  35.   ?>
  36.   <label>Sub Title: </label>
  37.   <input type="text" name="sub" value="<?php echo $sub; ?>" />
  38.   <?php
  39. }
  40.  
  41. function recipe_notesandmethods_meta() {
  42.   global $post;
  43.   $custom = get_post_custom($post->ID);
  44.   $notes = $custom["notes"][0];
  45.   $serving = $custom["serving"][0];
  46.   $directions = $custom["directions"][0];
  47.   ?>
  48.   <table>
  49.     <tr>
  50.     <td style="vertical-align: top;"><label>Notes: </label></td>
  51.     <td><textarea cols="50" rows="10" type="text" name="notes"><?php echo $notes;?></textarea></td>
  52.     </tr>
  53.     <tr>
  54.     <td style="vertical-align: top;"><label>Serving size: </label></td>
  55.     <td><input type="text" name="serving" value="<?php echo $serving; ?>" /></td>
  56.     </tr>
  57.     <tr>
  58.     <td style="vertical-align: top;"><label>Method: </label></td>
  59.     <td><textarea cols="50" rows="10" type="text" name="directions"><?php echo $directions;?></textarea></td>
  60.     </tr>
  61.   </table>
  62.  <?php
  63. }
  64.  
  65. function recipe_ingqty_meta() {
  66.     global $post;
  67.     $ingredients = get_post_meta( $post->ID, '_recipe_ingredients', true );
  68.     echo '<div id="recipe_ingredients">';
  69.     foreach ( $ingredients as $ingredient ) {
  70.         echo '<p><label>Ingredient: <input name="recipe_ingredients[]" type="text" value="' . esc_attr( $ingredient[ 0 ] ) . '" /></label>';
  71.         echo ' <label>Qty: <input name="recipe_qty[]" type="text" value="' . esc_attr( $ingredient[ 1 ] ) . '" /></label> <a href="#" class="remove button">Remove</a></p>';
  72.     }
  73.     echo '</div><p><a href="#" class="button-primary" id="recipe_new_ingredient">New ingredient</a></p>';
  74. }
  75.  
  76. function recipe_post_scripts() {
  77.     if ( $GLOBALS['post_type'] != 'post' )
  78.         return;
  79. $js = <<<JS
  80. <script type="text/javascript">
  81. jQuery(document).ready( function($) {
  82.     $('#recipe_new_ingredient').click( function() {
  83.         $('#recipe_ingredients').append( '<p><label>Ingredient: <input name="recipe_ingredients[]" type="text" /></label> <label>Qty: <input name="recipe_qty[]" type="text" /></label> <a href="#" class="remove button">Remove</a></p>' );
  84.         return false;
  85.     });
  86.     $('#recipe_ingredients .remove').live('click', function() {
  87.         $(this).parent().remove();
  88.         return false;
  89.     });
  90. });
  91. </script>
  92. JS;
  93. echo $js;
  94. }
  95.  
  96. add_action( 'admin_init', 'add_recipe_ingqty_meta_box' );
  97. function add_recipe_ingqty_meta_box() {
  98.     add_meta_box( 'recipe_meta', 'Ingredients And Qty.', 'recipeingqty_meta', 'post', 'normal', 'high' );
  99.     add_action( 'admin_footer-post.php', 'recipe_post_scripts' );
  100.     add_action( 'admin_footer-post-new.php', 'recipe_post_scripts' );
  101. }
  102.  
  103. add_action("admin_init", "recipe_admin_init");
  104. add_action('save_post', 'post_save_details');
  105.  
  106. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement