As with anything in WordPress it seems there are 20 way to do something, but after several weeks of extensive searching I really haven’t found more then a couple tutorials floating around that really mirror each other in writing conventions which always makes me a little uneasy when this is the way I learn so I wanted to make a post and ask you guys, the community, what you think?….
Even if you just have a favorite resource such as a tutorial on the web, that would be very useful. I feel Like Ive combed over them all, but often it seems like asking a specific group of experienced people typically renders better results so here I am.
*PS – Ive tried DeluxeBlogs class, and it worked fine, but at the moment what Im really trying to do is learn the most “correct” way to go about writing my own meta boxes from scratch.
Thanks,
SB
—– EDIT 1 : Example of one of my current meta boxes —–
<?php
// Register Custom Post Type Meta Boxes -- Description Field
add_action( 'add_meta_boxes', 'store_description_meta_box_add' );
function store_description_meta_box_add()
{
add_meta_box( 'epr_store_description_meta_id', 'Product Description', 'store_description_meta_box_cb', 'epr_store', 'normal', 'high' );
}
function store_description_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$textbox = isset( $values['meta_box_description_text'] ) ? esc_attr( $values['meta_box_description_text'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<textarea name="meta_box_description_text" id="meta_box_description_text" style="width:100%; height:100px;text-align:left;">
<?php echo $textbox; ?>
</textarea>
<?php
}
add_action( 'save_post', 'epr_description_meta_box_save' );
function epr_description_meta_box_save( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post' ) ) return;
$allowed = array(
'a' => array(
'href' => array()
)
);
if( isset( $_POST['meta_box_description_text'] ) )
update_post_meta( $post_id, 'meta_box_description_text', wp_kses( $_POST['meta_box_description_text'], $allowed ) );
}
?>
—– EDIT 2 : Example of how to remove indentation after saving w/ Chris’ help —–
<?php
// Register Custom Post Type Meta Boxes -- Description Field
add_action( 'add_meta_boxes', 'store_description_meta_box_add' );
function store_description_meta_box_add()
{
add_meta_box( 'epr_store_description_meta_id', 'Product Description', 'store_description_meta_box_cb', 'epr_store', 'normal', 'high' );
}
function store_description_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$textbox = isset( $values['meta_box_description_text'] ) ? esc_attr( $values['meta_box_description_text'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<textarea name="meta_box_description_text" id="meta_box_description_text" style="width:100%; height:100px;text-align:left;"><?php echo esc_attr($textbox); ?></textarea>
<?php
}
add_action( 'save_post', 'epr_description_meta_box_save' );
function epr_description_meta_box_save( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post' ) ) return;
$allowed = array(
'a' => array(
'href' => array()
)
);
if( isset( $_POST['meta_box_description_text'] ) )
update_post_meta( $post_id, 'meta_box_description_text', wp_kses( $_POST['meta_box_description_text'], $allowed ) );
}
?>
I have a sort of “plugin library” that contains a base class for meta boxes. Basically all it contains are a bunch of methods that generate form fields and some tabbed navigation stuff that I do for meta boxes that contain a lot of stuff. This just let’s me more rapidly develop the meta box without having to worry about coding form fields for hours.
I’ll use that as a parent class for a class that actually adds the meta boxes. That class would look something like this:
The most painful part of creating a meta box is writing the actual meta box call back function that outputs all the contents. It takes the longest, and it seemed like that was the thing that could most easily be more abstracted. So that’s what I did. Other people will probably have much different opinions.
All that said: sometimes meta boxes (and any other part of a plugin/theme) don’t fit into the mold. You have to know when to abandon the stuff that makes it easy in favor of what the situation demands. Don’t get locked into your tools just because you created them.
This tutorial might interest you as well. It doesn’t have anything like the above, just a basic how to.
I’m not big on plugins when editing the theme function file is an option, but the More Fields plugin saves so much time, that I use that instead. It’s quick, easy, and gives me a lot of options in creating different types of fields grouped in metaboxes that can be assigned to specific post types.