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
Posted in wordpress | Leave a comment

CSS gradient background

With CSS3 you don’t have to use images to create nice gradient backgrounds, although not all browsers support this feature yet, why don’t take advantage of the browsers that do… Chrome, Safari, Firefox 3.6…. IE needs some special code, but even IE6 supports CSS gradient!


background:#ebd3ac; /* regular browsers that don't support this feature */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#feeed2', endColorstr='#ebd4ad'); /* IE */
background: -webkit-gradient(linear, left top, left bottom, from(#feeed2), to(#ebd4ad)); /* webkit (Chrome, safari) */
background: -moz-linear-gradient(top, #feeed2, #ebd4ad); /* firefox */
Posted in css | Leave a comment

Hello world!

Hello! This blog has been created as a personal blog/journal to gather some interesting tips and tricks about WordPress. We don’t have any rights on the information presented here, we’ll try to insert links to the original posts. If you feel that we took your information against your will – please let us know. Thank you.

Posted in Uncategorized | Leave a comment

Post thumbnails

WordPress¬† 2.9 and up supports post and page thumbnails. Now you don’t need custom fields or extra plugins. Simply open your functions.php and add

add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 50, 50, true );

50, 50 means width and height accordingly. Then insert the following code in any loop you want to display the thumbnail
<?php the_post_thumbnail(); ?>

More detailed discussion here

Posted in hacks | Leave a comment