Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action( 'admin_init', 'hhs_add_metaboxes' );
- function hhs_add_metaboxes() {
- add_meta_box('hhs_checkbox_metabox', 'Checkboxes as CSV', 'hhs_checkbox_metabox', 'post', 'side', 'core');
- }
- function hhs_checkbox_metabox() {
- global $post;
- wp_nonce_field( plugin_basename( __FILE__ ), 'hhs_metabox_nonce' );
- $checkboxes = get_post_meta( $post->ID, 'checkboxes', true );
- // this is just a check to see the CSV
- echo "<p>CSV: $checkboxes</p>";
- $checkboxes = explode( ',', $checkboxes );
- echo '<p><strong>Checkboxes:</strong></p>
- <p>';
- for ( $i = 1; $i < 7; $i++ ) {
- echo '<input type="checkbox" name="checkboxes[]" value="'.$i.'"';
- if ( in_array($i, $checkboxes) )
- echo ' checked="checked"';
- echo ' /> '.$i.'<br />';
- }
- echo '</p>';
- }
- // Save data from meta box
- add_action('save_post', 'hhs_checkbox_metabox_save');
- function hhs_checkbox_metabox_save($post_id) {
- // verify nonce
- if ( !wp_verify_nonce( $_POST['hhs_metabox_nonce'], plugin_basename( __FILE__ ) ) )
- return;
- // check autosave
- if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
- return;
- // check permissions
- if (!current_user_can('edit_post', $post_id))
- return;
- $old['checkboxes'] = get_post_meta( $post_id, 'checkboxes', true );
- $new['checkboxes'] = $_POST['checkboxes'];
- $new['checkboxes'] = implode( ',', $new['checkboxes'] );
- if ( $new['checkboxes'] && $new['checkboxes'] != $old['checkboxes'] ) {
- update_post_meta($post_id, 'checkboxes', $new['checkboxes']);
- } elseif ( '' == $new['checkboxes'] && $old['checkboxes'] ) {
- delete_post_meta($post_id, 'checkboxes', $old['checkboxes']);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement