Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Metabox para caracteristicas externas de vehiculos Archivo de funciones para metabox
- $prefix = 'csp_';
- $ext_post_meta_box = array(
- 'id' => 'ext-post-meta-box',
- 'title' => __('Exterior', 'classyperu'),
- 'page' => 'post',
- 'context' => 'normal',
- 'priority' => 'high',
- 'fields' => array(
- array(
- 'name' => 'Limpiadores',
- 'id' => $prefix . 'limpiadores',
- 'type' => 'checkbox'
- ),
- array(
- 'name' => 'Llantas de Aleación',
- 'id' => $prefix . 'llantas',
- 'type' => 'checkbox'
- ),
- array(
- 'name' => 'Parachoques Pintados',
- 'id' => $prefix . 'parachoques',
- 'type' => 'checkbox'
- ),
- array(
- 'name' => 'Lunas Polarizadas',
- 'id' => $prefix . 'polarizados',
- 'type' => 'checkbox'
- )
- )
- );
- add_action( 'add_meta_boxes', 'ext_project_add_meta');
- function ext_project_add_meta() {
- global $ext_post_meta_box;
- 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']);
- }
- function ext_display_post_meta() {
- global $ext_post_meta_box, $post;
- echo '<input type="hidden" name="ext_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
- echo '<table class="form-table">';
- foreach ($ext_post_meta_box['fields'] as $field) {
- $meta = get_post_meta($post->ID, $field['id'], true);
- switch($field['type']) {
- // checkbox
- case 'checkbox':
- echo '<div style="float: left; margin-left: 10px"> <label for="'.$field['id'].'">'.$field['name'].'</label>
- <input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/></div>';
- break;
- }
- }
- echo '</table>';
- }
- add_action('save_post', 'ext_post_save_data');
- function ext_post_save_data($post_id) {
- global $ext_post_meta_box;
- if (!isset($_POST['ext_meta_box_nonce']) || !wp_verify_nonce($_POST['ext_meta_box_nonce'], basename(__FILE__))) {
- return $post_id;
- }
- if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
- return $post_id;
- }
- if ('page' == $_POST['post_type']) {
- if (!current_user_can('edit_page', $post_id)) {
- return $post_id;
- }
- } elseif (!current_user_can('edit_post', $post_id)) {
- return $post_id;
- }
- foreach ($ext_post_meta_box['fields'] as $field) {
- $old = get_post_meta($post_id, $field['id'], true);
- $new = $_POST[$field['id']];
- if ($new && $new != $old) {
- update_post_meta($post_id, $field['id'], $new);
- } elseif ('' == $new && $old) {
- delete_post_meta($post_id, $field['id'], $old);
- }
- }
- }
- ?>
- //Uso del metabox en el formulario de publicación en el fron end.
- <h4>Exterior:</h4>
- <fieldset>
- <input type="checkbox" value="on" name="csp_limpiadores" id="csp_limpiadores" <? $meta && in_array($option['csp_limpiadores'], $meta) ? ' checked="checked"' : ''?> />
- <label for="csp_limpiadores">Limpiadores</label><br />
- <input type="checkbox" value="on" name="csp_llantas" id="csp_llantas" <? $meta && in_array($option['csp_llantas'], $meta) ? ' checked="checked"' : ''?> />
- <label for="csp_llantas">Llantas de Aleación</label><br />
- <input type="checkbox" value="on" name="csp_parachoques" id="csp_parachoques" <? $meta && in_array($option['csp_parachoques'], $meta) ? ' checked="checked"' : ''?> />
- <label for="csp_parachoques">Parachoques Pintados</label><br />
- <input type="checkbox" value="on" name="csp_polarizados" id="csp_polarizados" <? $meta && in_array($option['csp_polarizados'], $meta) ? ' checked="checked"' : ''?> />
- <label for="csp_polarizados">Lunas Polarizadas</label>
- </fieldset>
- //Actualización en el archivo de procesos del formulario:
- if($post_id) {
- update_post_meta($post_id, 'csp_limpiadores', json_encode($_POST['csp_limpiadores']));
- update_post_meta($post_id, 'csp_llantas', json_encode($_POST['csp_llantas']));
- update_post_meta($post_id, 'csp_parachoques', json_encode($_POST['csp_parachoques']));
- update_post_meta($post_id, 'csp_polarizados', json_encode($_POST['csp_polarizados']));
- }
- //Esta función va antes del wp_direct()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement