Custom meta checkbox

$prefix = 'mcb_';

$meta_box = array(
    'id' => 'my-meta-box',
    'title' => 'Custom meta box',
    'page' => 'post',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
      
        array(
            'name' => 'Enable Byline',
            'id' => $prefix . 'checkbox',
			
            'type' => 'checkbox'
        )
    )
);

add_action('admin_menu', 'add_custom_box');

Adding our checkbox to admin area

function add_custom_box() {
    global $meta_box;
    
    add_meta_box('Byline', 'Enable Byline', 'show_custom_box', 'post', 'normal', 'high'); //shows up in posts
    add_meta_box('Byline', 'Enable Byline', 'show_custom_box', 'audio', 'normal', 'high'); // custom post type audio
    add_meta_box('Byline', 'Enable Byline', 'show_custom_box', 'video', 'normal', 'high'); // custom post type video

}

Callback function to show checkbox in meta box

function show_custom_box() {
global $meta_box, $post;

echo '';  //verify using nonce
foreach ($meta_box['fields'] as $field) {
$meta = get_post_meta($post->ID, $field['id'], true);
case 'checkbox':
                echo ' Want to enable byline?';
                break;
}
}
}

Now We need to save it in DB

add_action('save_post', 'mytheme_save_data');

// Save data from meta box
function mytheme_save_data($post_id) {
    global $meta_box;
    
    // verify nonce
    if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
    }

    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    // check permissions
    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 ($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);
        }
    }
}

And finally we need to display something in our template if checkbox is checked…

global $meta_box;
$byline = get_post_meta($post->ID, 'dbt_checkbox', true); 
if ($byline=='on') { ?> by 

More details on Deluxe Blog Tips
For more help and custom WordPress development visit NDM web site

Advertisements
This entry was posted in wordpress. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s