'my-meta-box-1', 'title' => 'Match details', 'pages' => array('post', 'matches'), // multiple post types, accept custom post types 'context' => 'normal', // normal, advanced, side (optional) 'priority' => 'high', // high, low (optional) 'fields' => array( array( 'name' => 'Enable match details', // check box 'id' => $prefix . 'checkbox', 'type' => 'checkbox' ), array( 'name' => 'Team 1', 'desc' => 'vs', 'id' => $prefix . 'team1', 'type' => 'text', // text box 'std' => '', ), array( 'name' => 'Team 2', 'desc' => '', 'id' => $prefix . 'team2', 'type' => 'text', // text box 'std' => '', ), array( 'name' => 'Match type', 'id' => $prefix . 'select', 'type' => 'select', // select box 'options' => array( // array of name, value pairs for select box array('name' => 'Single Elimation', 'value' => 'bo1'), array('name' => 'Best of 3', 'value' => 'bo3'), array('name' => 'Best of 5', 'value' => 'bo5') ) ), ) ); // second meta box $meta_boxes[] = array( 'id' => 'my-meta-box-2', 'title' => 'Champion picks and bans', 'pages' => array('post', 'matches'), // custom post types, since WordPress 3.0 'context' => 'normal', 'priority' => 'high', 'fields' => array( array( 'name' => 'Enable champion picks and bans', // check box 'id' => $prefix . 'checkboxpickbans', 'type' => 'checkbox' ), array( 'name' => 'Team 1 Champion Picks', 'id' => $prefix . 'select1', 'type' => 'champselect', // select box 'options' => array( // array of name, value pairs for select box array('name' => 'Alistar', 'value' => 'alistar'), array('name' => 'Ahri', 'value' => 'ahri'), array('name' => 'Sona', 'value' => 'sona'), array('name' => 'Annie', 'value' => 'annie'), array('name' => 'Urgot', 'value' => 'urgot'), array('name' => 'Wukong', 'value' => 'wukong') ) ), array( 'name' => 'Team 2 Champion Picks', 'id' => $prefix . 'select2', 'type' => 'champselect', // select box 'options' => array( // array of name, value pairs for select box array('name' => 'Alistar', 'value' => 'alistar'), array('name' => 'Ahri', 'value' => 'ahri'), array('name' => 'Sona', 'value' => 'sona'), array('name' => 'Annie', 'value' => 'annie'), array('name' => 'Urgot', 'value' => 'urgot'), array('name' => 'Wukong', 'value' => 'wukong') ) ), array( 'name' => 'Team 1 Champion Bans', 'id' => $prefix . 'select3', 'type' => 'champbans', // select box 'options' => array( // array of name, value pairs for select box array('name' => 'Alistar', 'value' => 'alistar'), array('name' => 'Ahri', 'value' => 'ahri'), array('name' => 'Sona', 'value' => 'sona'), array('name' => 'Annie', 'value' => 'annie'), array('name' => 'Urgot', 'value' => 'urgot'), array('name' => 'Wukong', 'value' => 'wukong') ) ), array( 'name' => 'Team 2 Champion Bans', 'id' => $prefix . 'select4', 'type' => 'champbans', // select box 'options' => array( // array of name, value pairs for select box array('name' => 'Alistar', 'value' => 'alistar'), array('name' => 'Ahri', 'value' => 'ahri'), array('name' => 'Sona', 'value' => 'sona'), array('name' => 'Annie', 'value' => 'annie'), array('name' => 'Urgot', 'value' => 'urgot'), array('name' => 'Wukong', 'value' => 'wukong') ) ), ) ); $meta_boxes[] = array( 'id' => 'my-meta-box-3', 'title' => 'VOD', 'pages' => array('post', 'matches'), // multiple post types, accept custom post types 'context' => 'normal', // normal, advanced, side (optional) 'priority' => 'high', // high, low (optional) 'fields' => array( array( 'name' => 'Enable VOD', // check box 'id' => $prefix . 'checkboxvod', 'type' => 'checkbox' ), array( 'name' => 'VOD Embed Code', 'desc' => 'Link to the matches VOD from own3d, Youtube, Twitch etc', 'id' => $prefix . 'textareavod', 'type' => 'textarea', // text area 'std' => '' ), ) ); foreach ($meta_boxes as $meta_box) { $my_box = new RW_Meta_Box($meta_box); } // Validate value of meta fields // Define ALL validation methods inside this class // and use the names of these methods in the definition of meta boxes (key 'validate_func' of each field) class RW_Meta_Box_Validate { function check_text($text) { if ($text != 'hello') { return false; } return true; } } /** * AJAX delete images on the fly. This script is a slightly altered version of a function used by the Plugin "Verve Meta Boxes" * http://wordpress.org/extend/plugins/verve-meta-boxes/ * */ add_action('wp_ajax_unlink_file', 'unlink_file_callback'); function unlink_file_callback() { global $wpdb; if ($_POST['data']) { $data = explode('-', $_POST['data']); $att_id = $data[0]; $post_id = $data[1]; wp_delete_attachment($att_id); } } /** * Create meta boxes */ class RW_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 $upload = false; foreach ($meta_box['fields'] as $field) { if ($field['type'] == 'file' || $field['type'] == 'image') { $upload = true; break; } } $current_page = substr(strrchr($_SERVER['PHP_SELF'], '/'), 1, -4); if ($upload && ($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_head', array(&$this, 'add_unlink_script')); add_action('admin_head', array(&$this, 'add_clone_script')); } add_action('admin_menu', array(&$this, 'add')); add_action('save_post', array(&$this, 'save')); } function add_post_enctype() { echo ' '; } function add_unlink_script(){ echo ' '; } function add_clone_script() { echo ' '; } /// Add meta box for multiple post types function add() { $this->_meta_box['context'] = empty($this->_meta_box['context']) ? 'normal' : $this->_meta_box['context']; $this->_meta_box['priority'] = empty($this->_meta_box['priority']) ? 'high' : $this->_meta_box['priority']; 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; // 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 '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 'wysiwyg': echo '', '
', $field['desc']; break; case 'champselect': echo ''; echo ''; echo ''; echo ''; echo ''; break; case 'champbans': echo ''; echo ''; echo ''; break; case 'image': ?>

Images attached to this post

'attachment', 'post_parent' => $post->ID, 'numberposts' => -1, 'post_status' => NULL ); $attachs = get_posts($args); if (!empty($attachs)) { foreach ($attachs as $att) { ?>
ID, 'thumbnail'); ?>
Delete Image

Upload new Images

', '
'; } // Save data from meta box function save($post_id) { // verify nonce if (!wp_verify_nonce($_POST['wp_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 ($this->_meta_box['fields'] as $field) { $name = $field['id']; $old = get_post_meta($post_id, $name, true); $new = $_POST[$field['id']]; /* // changed to using WP gallery if ($field['type'] == 'file' || $field['type'] == 'image') { $file = wp_handle_upload($_FILES[$name], array('test_form' => false)); $new = $file['url']; } */ if ($field['type'] == 'file' || $field['type'] == 'image') { if (!empty($_FILES[$name])) { $this->fix_file_array($_FILES[$name]); foreach ($_FILES[$name] as $position => $fileitem) { $file = wp_handle_upload($fileitem, array('test_form' => false)); $filename = $file['url']; if (!empty($filename)) { $wp_filetype = wp_check_filetype(basename($filename), null); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment($attachment, $filename, $post_id); // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once(ABSPATH . 'wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata($attach_id, $filename); wp_update_attachment_metadata($attach_id, $attach_data); } } } } if ($field['type'] == 'wysiwyg') { $new = wpautop($new); } if ($field['type'] == 'textarea') { $new = htmlspecialchars($new); } // validate meta value if (isset($field['validate_func'])) { $ok = call_user_func(array('RW_Meta_Box_Validate', $field['validate_func']), $new); if ($ok === false) { // pass away when meta value is invalid continue; } } 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); } } } /** * Fixes the odd indexing of multiple file uploads from the format: * * $_FILES['field']['key']['index'] * * To the more standard and appropriate: * * $_FILES['field']['index']['key'] * * @param array $files * @author Corey Ballou * @link http://www.jqueryin.com */ function fix_file_array(&$files) { $names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1 ); foreach ($files as $key => $part) { // only deal with valid keys and multiple files $key = (string) $key; if (isset($names[$key]) && is_array($part)) { foreach ($part as $position => $value) { $files[$position][$key] = $value; } // remove old key reference unset($files[$key]); } } } } ?>