Cleanest way to code “Custom Write Panels/Meta Boxes”?

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:

<?php
class wpseMetaBox extends davispressMetaBoxTools
{
    function __construct()
    {
        add_action( 'add_meta_boxes', array( &$this, 'add_meta_box' ) );
        add_action( 'save_post', array( &$this, 'save' ), 10, 1 );
        add_action( 'load-edit.php', array( &$this, 'add_styles_scripts' ) );
        add_action( 'load-post-new.php', array( &$this, 'add_styles_scripts' ) );
    }

    function add_meta_box()
    {
        add_meta_box( 'some-metabox', 'Meta Box Title', array( &$this, 'meta_box_cb' ), 'post' );
    }

    function meta_box_cb( $post )
    {
        wp_nonce_field( 'wpse_nonce', 'wpse_nonce' );
        // add fields here
        $o = $this->textinput( '_some_id', 'Some Label', $post->ID );
        echo $this->form_table( $o );
        // etc.
    }

    function save( $post_id )
    {
        // verify we can do this
        if( ! isset( $_REQUEST['wpse_nonce'] ) || ! wp_verify_nonce( $_REQUEST['wpse_nonce'], 'wpse_nonce' ) ) return;
        if( ! current_user_can( 'edit_post' ) ) return;

        // save data
        if( isset( $_REQUEST['_some_id'] ) )
            update_post_meta( $post_id, '_some_id', esc_attr( $_REQUEST['_some_id'] ) );
    }

    function add_styles_scripts()
    {
        // check and see if we're on the post type with the meta box
        if( ( isset( $_REQUEST['post'] ) && 'post' == get_post_type( $_REQUEST['post'] ) ) || ( isset( $_REQUEST['post_type'] ) && 'post' == $_REQUEST['post_type'] ) ) 
        {
            add_action( 'admin_print_scripts', array( &$this, 'scripts' ) );
            add_action( 'admin_print_styles', array( &$this, 'styles' ) );
        }
    }

    function scripts()
    {
        // wp_enqueue_script here
    }

    function styles()
    {
        // wp_enqueue_style here
    }
}

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.

Leave a Comment