Advertisement
hhsandi

Checkbox CSV Metabox

Sep 7th, 2011
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.66 KB | None | 0 0
  1. <?php
  2. add_action( 'admin_init', 'hhs_add_metaboxes' );
  3. function hhs_add_metaboxes() {
  4.   add_meta_box('hhs_checkbox_metabox', 'Checkboxes as CSV', 'hhs_checkbox_metabox', 'post', 'side', 'core');
  5. }
  6.  
  7. function hhs_checkbox_metabox() {
  8.     global $post;
  9.    
  10.     wp_nonce_field( plugin_basename( __FILE__ ), 'hhs_metabox_nonce' );
  11.        
  12.     $checkboxes = get_post_meta( $post->ID, 'checkboxes', true );
  13.    
  14.     // this is just a check to see the CSV
  15.     echo "<p>CSV: $checkboxes</p>";
  16.    
  17.     $checkboxes = explode( ',', $checkboxes );
  18.    
  19.     echo '<p><strong>Checkboxes:</strong></p>
  20.     <p>';
  21.    
  22.     for ( $i = 1; $i < 7; $i++ ) {
  23.         echo '<input type="checkbox" name="checkboxes[]" value="'.$i.'"';
  24.        
  25.         if ( in_array($i, $checkboxes) )
  26.             echo ' checked="checked"';
  27.        
  28.         echo ' /> '.$i.'<br />';
  29.     }
  30.    
  31.     echo '</p>';
  32. }
  33.  
  34. // Save data from meta box
  35. add_action('save_post', 'hhs_checkbox_metabox_save');
  36. function hhs_checkbox_metabox_save($post_id) {
  37.   // verify nonce
  38.   if ( !wp_verify_nonce( $_POST['hhs_metabox_nonce'], plugin_basename( __FILE__ ) ) )
  39.     return;
  40.  
  41.   // check autosave
  42.   if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
  43.     return;
  44.  
  45.   // check permissions
  46.   if (!current_user_can('edit_post', $post_id))
  47.     return;
  48.  
  49.     $old['checkboxes'] = get_post_meta( $post_id, 'checkboxes', true );
  50.     $new['checkboxes'] = $_POST['checkboxes'];
  51.     $new['checkboxes'] = implode( ',', $new['checkboxes'] );
  52.    
  53.     if ( $new['checkboxes'] && $new['checkboxes'] != $old['checkboxes'] ) {
  54.       update_post_meta($post_id, 'checkboxes', $new['checkboxes']);
  55.     } elseif ( '' == $new['checkboxes'] && $old['checkboxes'] ) {
  56.       delete_post_meta($post_id, 'checkboxes', $old['checkboxes']);
  57.     }
  58. }
  59. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement