'my-meta-box-1', 'title' => 'Custom meta box 1', 'pages' => array('post', 'page', 'link'), // multiple post types 'context' => 'normal', 'priority' => 'high', 'fields' => array( array( 'name' => 'Text box', 'desc' => 'Enter something here', 'id' => $prefix . 'text', 'type' => 'text', // text box 'std' => 'Default value 1' ), array( 'name' => 'Textarea', 'desc' => 'Enter big text here', 'id' => $prefix . 'textarea', 'type' => 'textarea', // text area 'std' => 'Default value 2' ), array( 'name' => 'Tiny MCE', 'desc' => 'Enter big text here', 'id' => $prefix . 'tinymce', 'type' => 'tinymce', // tinymce 'std' => 'Default value 2' ), array( 'name' => 'Select box', 'id' => $prefix . 'select', 'type' => 'select', // select box 'options' => array('Option 1', 'Option 2', 'Option 3') // array of options for select box ), array( 'name' => 'Radio', 'id' => $prefix . 'radio', 'type' => 'radio', // radio box 'options' => array( // array of name, value pairs for radio options array('name' => 'Name 1', 'value' => 'Value 1'), array('name' => 'Name 2', 'value' => 'Value 2') ) ), array( 'name' => 'Checkbox', // check box 'id' => $prefix . 'checkbox', 'type' => 'checkbox' ) ) ); // second meta box $meta_boxes[] = array( 'id' => 'my-meta-box-2', 'title' => 'Custom meta box 2', 'pages' => array('post', 'film', 'album'), // custom post types, since WordPress 3.0 'context' => 'normal', 'priority' => 'high', 'fields' => array( array( 'name' => 'File upload', 'desc' => 'For file upload', 'id' => $prefix . 'file', 'type' => 'file' // file upload ), array( 'name' => 'Image upload', 'desc' => 'For image upload', 'id' => $prefix . 'image', 'type' => 'image' // image upload ), ) ); /********************************* You should not edit the code below *********************************/ foreach ($meta_boxes as $meta_box) { $my_box = new My_meta_box($meta_box); } class My_meta_box { protected $_meta_box; // create meta box based on given data function __construct($meta_box) { if (!is_admin()) return; $this->_meta_box = $meta_box; // fix upload bug: http://www.hashbangcode.com/blog/add-enctype-wordpress-post-and-page-forms-471.html $current_page = substr(strrchr($_SERVER['PHP_SELF'], '/'), 1, -4); if ($current_page == 'page' || $current_page == 'page-new' || $current_page == 'post' || $current_page == 'post-new') { add_action('admin_head', array(&$this, 'add_post_enctype')); } add_action('admin_menu', array(&$this, 'add')); add_action('save_post', array(&$this, 'save')); } function add_post_enctype() { echo ' '; } /// Add meta box for multiple post types function add() { foreach ($this->_meta_box['pages'] as $page) { add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']); } } // Callback function to show fields in meta box function show() { global $post; $mce_ids = array(); // Use nonce for verification echo ''; echo ''; foreach ($this->_meta_box['fields'] as $field) { // get current post meta data $meta = get_post_meta($post->ID, $field['id'], true); echo '', '', ''; } echo '
'; switch ($field['type']) { case 'text': echo '', '
', $field['desc']; break; case 'textarea': echo '', '
', $field['desc']; break; case 'tinymce': array_push($mce_ids, $field['id']); echo '
'; echo '', '
'; echo '
'; echo $field['desc']; break; case 'select': echo ''; break; case 'radio': foreach ($field['options'] as $option) { echo '', $option['name']; } break; case 'checkbox': echo ''; break; case 'file': echo $meta ? "$meta
" : '', '', '
', $field['desc']; break; case 'image': echo $meta ? "
$meta
" : '', '', '
', $field['desc']; break; } echo '
', '
'; // If we have accumulated any TinyMCE type items, then active TinyMCE on them if (count($mce_ids) > 0) { ?> _meta_box['fields'] as $field) { $name = $field['id']; $old = get_post_meta($post_id, $name, true); $new = $_POST[$field['id']]; if ($field['type'] == 'file' || $field['type'] == 'image') { $file = wp_handle_upload($_FILES[$name], array('test_form' => false)); $new = $file['url']; } if ($new && $new != $old) { update_post_meta($post_id, $name, $new); } elseif ('' == $new && $old && $field['type'] != 'file' && $field['type'] != 'image') { delete_post_meta($post_id, $name, $old); } } } } ?>