Advertisement
Fliberty

Metabox con chexboxes

Oct 3rd, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.53 KB | None | 0 0
  1. <?php
  2. //Metabox para caracteristicas externas de vehiculos Archivo de funciones para metabox
  3. $prefix = 'csp_';
  4. $ext_post_meta_box = array(
  5.     'id' => 'ext-post-meta-box',
  6.     'title' => __('Exterior', 'classyperu'),
  7.     'page' => 'post',
  8.     'context' => 'normal',
  9.     'priority' => 'high',
  10.     'fields' => array(
  11.         array(
  12.             'name' => 'Limpiadores',
  13.             'id' => $prefix . 'limpiadores',
  14.             'type' => 'checkbox'
  15.             ),
  16.         array(
  17.             'name' => 'Llantas de Aleaci&oacute;n',
  18.             'id' => $prefix . 'llantas',
  19.             'type' => 'checkbox'
  20.             ),
  21.         array(
  22.             'name' => 'Parachoques Pintados',
  23.             'id' => $prefix . 'parachoques',
  24.             'type' => 'checkbox'
  25.             ),
  26.         array(
  27.             'name' => 'Lunas Polarizadas',
  28.             'id' => $prefix . 'polarizados',
  29.             'type' => 'checkbox'
  30.             )
  31.         )
  32.     );
  33.  
  34. add_action( 'add_meta_boxes', 'ext_project_add_meta');
  35. function ext_project_add_meta() {
  36.     global $ext_post_meta_box;
  37.     add_meta_box($ext_post_meta_box['id'], $ext_post_meta_box['title'], 'ext_display_post_meta', $ext_post_meta_box['page'], $ext_post_meta_box['context'], $ext_post_meta_box['priority']);
  38. }
  39.  
  40. function ext_display_post_meta() {
  41.     global $ext_post_meta_box, $post;
  42.     echo '<input type="hidden" name="ext_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  43.     echo '<table class="form-table">';
  44.     foreach ($ext_post_meta_box['fields'] as $field) {
  45.         $meta = get_post_meta($post->ID, $field['id'], true);
  46.         switch($field['type']) {
  47.         // checkbox
  48.         case 'checkbox':
  49.         echo '<div style="float: left; margin-left: 10px"> <label for="'.$field['id'].'">'.$field['name'].'</label>
  50.        <input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/></div>';
  51.         break;
  52.         }
  53.     }
  54.     echo '</table>';
  55. }
  56.  
  57. add_action('save_post', 'ext_post_save_data');
  58. function ext_post_save_data($post_id) {
  59.     global $ext_post_meta_box;
  60.     if (!isset($_POST['ext_meta_box_nonce']) || !wp_verify_nonce($_POST['ext_meta_box_nonce'], basename(__FILE__))) {
  61.         return $post_id;
  62.     }
  63.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  64.         return $post_id;
  65.     }
  66.     if ('page' == $_POST['post_type']) {
  67.         if (!current_user_can('edit_page', $post_id)) {
  68.             return $post_id;
  69.         }
  70.     } elseif (!current_user_can('edit_post', $post_id)) {
  71.         return $post_id;
  72.     }
  73.     foreach ($ext_post_meta_box['fields'] as $field) {
  74.         $old = get_post_meta($post_id, $field['id'], true);
  75.         $new = $_POST[$field['id']];
  76.         if ($new && $new != $old) {
  77.             update_post_meta($post_id, $field['id'], $new);
  78.         } elseif ('' == $new && $old) {
  79.             delete_post_meta($post_id, $field['id'], $old);
  80.         }
  81.     }
  82. }
  83. ?>
  84.  
  85. //Uso del metabox en el formulario de publicación en el fron end.
  86. <h4>Exterior:</h4>
  87. <fieldset>
  88.     <input type="checkbox" value="on" name="csp_limpiadores" id="csp_limpiadores" <? $meta && in_array($option['csp_limpiadores'], $meta) ? ' checked="checked"' : ''?> />
  89.     <label for="csp_limpiadores">Limpiadores</label><br />
  90.  
  91.     <input type="checkbox" value="on" name="csp_llantas" id="csp_llantas" <? $meta && in_array($option['csp_llantas'], $meta) ? ' checked="checked"' : ''?> />
  92.     <label for="csp_llantas">Llantas de Aleaci&oacute;n</label><br />
  93.    
  94.     <input type="checkbox" value="on" name="csp_parachoques" id="csp_parachoques" <? $meta && in_array($option['csp_parachoques'], $meta) ? ' checked="checked"' : ''?> />
  95.     <label for="csp_parachoques">Parachoques Pintados</label><br />
  96.    
  97.     <input type="checkbox" value="on" name="csp_polarizados" id="csp_polarizados" <? $meta && in_array($option['csp_polarizados'], $meta) ? ' checked="checked"' : ''?> />
  98.     <label for="csp_polarizados">Lunas Polarizadas</label>
  99. </fieldset>
  100.  
  101. //Actualización en el archivo de procesos del formulario:
  102.         if($post_id) {
  103.             update_post_meta($post_id, 'csp_limpiadores', json_encode($_POST['csp_limpiadores']));  
  104.             update_post_meta($post_id, 'csp_llantas', json_encode($_POST['csp_llantas']));
  105.             update_post_meta($post_id, 'csp_parachoques', json_encode($_POST['csp_parachoques']));
  106.             update_post_meta($post_id, 'csp_polarizados', json_encode($_POST['csp_polarizados']));
  107.         }
  108. //Esta función va antes del wp_direct()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement