This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

custom meta box

By: a guest on Sep 28th, 2011  |  syntax: PHP  |  size: 2.09 KB  |  views: 113  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. add_action('add_meta_boxes', 'lonchera_add_infoproducto');
  3.  
  4. function lonchera_add_infoproducto()
  5. {
  6.         add_meta_box('lonchera_infoproducto', 'Informaci&oacute;n del producto', 'lonchera_infoproducto', 'post', 'normal', 'default');
  7. }
  8.  
  9. function lonchera_infoproducto() {
  10.     global $post, $wp_locale;
  11.  
  12.     wp_nonce_field( plugin_basename(__FILE__), 'lonchera_infoproducto_noncename' );
  13.        
  14.         $codigo = get_post_meta($post->ID, 'codigo', true);
  15.        
  16.         $lol = '<label for="marca">C&oacute;digo:</label> <input type="text" name="lonchera_codigo" value="'.$codigo.'" id="lonchera_marca" />';
  17.        
  18.         echo $lol;
  19. }
  20.  
  21. function lonchera_save_infoproducto($post_id, $post) {
  22.  
  23.     if ( !wp_verify_nonce( $_POST['lonchera_infoproducto_noncename'], plugin_basename(__FILE__) )) {
  24.         return $post_id;
  25.         }
  26.  
  27.         // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
  28.         // to do anything
  29.         if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
  30.                 return $post_id;
  31.         }
  32.  
  33.  
  34.         // Check permissions
  35.         if ( 'page' == $_POST['post_type'] ) {
  36.                 if ( !current_user_can( 'edit_page', $post_id ) )
  37.                 return $post_id;
  38.         } else {
  39.                 if ( !current_user_can( 'edit_post', $post_id ) )
  40.                 return $post_id;
  41.         }
  42.  
  43.     // OK, we're authenticated: we need to find and save the data
  44.     // We'll put it into an array to make it easier to loop though.
  45.  
  46.     $metas['codigo'] = $_POST['lonchera_codigo'];
  47.        
  48.         foreach ($metas as $key => $value) { // Cycle through the $events_meta array!
  49.         if( $post->post_type == 'revision' ) return; // Don't store custom data twice
  50.         $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
  51.         if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
  52.             update_post_meta($post->ID, $key, $value);
  53.         } else { // If the custom field doesn't have a value
  54.             add_post_meta($post->ID, $key, $value);
  55.         }
  56.         //if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
  57.     }
  58.        
  59.        
  60. }
  61.  
  62. add_action('save_post', 'lonchera_save_infoproducto', 1, 2);
  63. ?>
  64.  
clone this paste RAW Paste Data