Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Create meta boxes for editing pages in WordPress
- * Compatible with custom post types in WordPress 3.0
- *
- * Support input types: text, textarea, checkbox, radio box, select, file, image
- *
- * @author: Rilwis
- * @url: http://www.deluxeblogtips.com/2010/04/how-to-create-meta-box-wordpress-post.html
- * @version: 2.4.1
- *
- * Changelog:
- * - 2.4.1: fix bug of not receiving value for select box
- * - 2.4: (image upload features are credit to Kai http://twitter.com/ungestaltbar)
- * + change image upload using meta fields to using default WP gallery
- * + add delete button for images, using ajax
- * + allow to upload multiple images
- * + add validation for meta fields
- * - 2.3: add wysiwyg editor type, improve check for upload fields, change context and priority attributes to optional
- * - 2.2: add enctype to post form (fix upload bug), thanks to http://www.hashbangcode.com/blog/add-enctype-wordpress-post-and-page-forms-471.html
- * - 2.1: add file upload, image upload support
- * - 2.0: oop code, support multiple post types, multiple meta boxes
- * - 1.0: procedural code
- */
- /*
- * Edits added by Funkatron
- * Changelog:
- *-Date feature added. Creates 3 select boxes corresponding with month, day and year and saves in 3 custom fields by taking the id and appending with '_month',
- * '_day', or '_year'. year_range feature added to set a range of years. See example below for usage.
- *-Taxonomy feature added. Id corresponds with name of already precreated taxonomy. Can be set to either 'taxonomy-select' to mimic categories or
- * 'taxonomy-checkbox' to mimic tags. Autopopulates from existing taxonomy terms.
- */
- /*
- Usage: for more information, please visit: http://www.deluxeblogtips.com/2010/04/how-to-create-meta-box-wordpress-post.html
- // Register meta boxes
- $prefix = 'dbt_';
- $meta_boxes = array();
- // first meta box
- $meta_boxes[] = array(
- 'id' => 'my-meta-box-1',
- 'title' => 'Custom meta box 1',
- 'pages' => array('post', 'page', 'link'), // multiple post types, accept custom post types
- 'context' => 'normal', // normal, advanced, side (optional)
- 'priority' => 'high', // high, low (optional)
- 'fields' => array(
- array(
- 'name' => 'Text box',
- 'desc' => 'Enter something here',
- 'id' => $prefix . 'text',
- 'type' => 'text', // text box
- 'std' => 'Default value 1',
- 'validate_func' => 'check_text' // validate function, created below, inside RW_Meta_Box_Validate class
- ),
- array(
- 'name' => 'Textarea',
- 'desc' => 'Enter big text here',
- 'id' => $prefix . 'textarea',
- 'type' => 'textarea', // text area
- 'std' => 'Default value 2'
- ),
- array(
- 'name' => 'Select box',
- 'id' => $prefix . 'select',
- 'type' => 'select', // select box
- 'options' => array( // array of name, value pairs for select box
- array('name' => 'Name 1', 'value' => 'Value 1'),
- array('name' => 'Name 2', 'value' => 'Value 2')
- )
- ),
- 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' => 'Rich editor',
- 'id' => $prefix . 'wysiwyg',
- 'type' => 'wysiwyg', // WYSIWYG editor
- 'std' => 'Default value 2'
- ),
- 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
- ),
- )
- );
- //New metabox elements: date, taxonomy select and taxonomy checkboxes
- metaboxes[] = 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' => 'Due Date:',
- 'desc' => 'date is due',
- 'id' => 'due_date',
- 'type' => 'date',
- 'year_range' => array('min'=>1990, max => 2011)
- ),
- array(
- 'name' => 'Places',
- 'desc' => 'Enter something here',
- 'id' => 'places',
- 'type' => 'taxonomy-select'
- ),
- array(
- 'name' => 'People',
- 'desc' => 'Enter something here',
- 'id' => 'people',
- 'type' => 'taxonomy-checkbox'
- )
- )
- );
- 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 '
- <script type="text/javascript">
- jQuery(document).ready(function(){
- jQuery("#post").attr("enctype", "multipart/form-data");
- jQuery("#post").attr("encoding", "multipart/form-data");
- });
- </script>';
- }
- function add_unlink_script(){
- echo '
- <script type="text/javascript">
- jQuery(document).ready(function($){
- $("a.deletefile").click(function () {
- var parent = jQuery(this).parent(),
- data = jQuery(this).attr("rel"),
- _wpnonce = $("input[name=\'_wpnonce\']").val();
- $.post(
- ajaxurl,
- {action: \'unlink_file\', _wpnonce: _wpnonce, data: data},
- function(response){
- //$("#info").html(response).fadeOut(3000);
- //alert(data.post);
- },
- "json"
- );
- parent.fadeOut("slow");
- return false;
- });
- });
- </script>';
- }
- function add_clone_script() {
- echo '
- <script type="text/javascript">
- jQuery(document).ready(function() {
- jQuery(".add").click(function() {
- jQuery("#newimages p:first-child").clone().insertAfter("#newimages p:last").show();
- return false;
- });
- jQuery(".remove").click(function() {
- jQuery(this).parent().remove();
- });
- });
- </script>';
- }
- /// 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 '<input type="hidden" name="wp_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
- echo '<table class="form-table">';
- foreach ($this->_meta_box['fields'] as $field) {
- // get current post meta data
- $meta = get_post_meta($post->ID, $field['id'], true);
- echo '<tr>',
- '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
- '<td>';
- switch ($field['type']) {
- case 'text':
- echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
- '<br />', $field['desc'];
- break;
- case 'textarea':
- echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="15" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
- '<br />', $field['desc'];
- break;
- case 'select':
- echo '<select name="', $field['id'], '" id="', $field['id'], '">';
- foreach ($field['options'] as $option) {
- echo '<option value="', $option['value'], '"', $meta == $option['value'] ? ' selected="selected"' : '', '>', $option['name'], '</option>';
- }
- echo '</select>';
- break;
- case 'date':
- $meta_month = get_post_meta($post->ID, $field['id'].'_month', true);
- $meta_day = get_post_meta($post->ID, $field['id'].'_day', true);
- $meta_year = get_post_meta($post->ID, $field['id'].'_year', true);
- $years = range(isset($field['year_range']['max']) ? $field['year_range']['max'] :(int) date('Y'), isset($field['year_range']['min']) ? $field['year_range']['min'] : 1950);
- $months = array(1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
- $days = range(1,31);
- echo '<select name="', $field['id'], '_month" id="', $field['id'], '_month">';
- foreach ($months as $month => $mvalue) {
- echo '<option value="', $month, '"', (int) $meta_month == (int) $month ? ' selected="selected"' : '', '>', $mvalue, '</option>';
- }
- echo '</select>';
- echo '<select name="', $field['id'], '_day" id="', $field['id'], '_day">';
- foreach ($days as $day) {
- echo '<option value="', $day, '"', (int) $meta_day == (int) $day ? ' selected="selected"' : '', '>', $day, '</option>';
- }
- echo '</select>';
- echo '<select name="', $field['id'], '_year" id="', $field['id'], '_year">';
- foreach ($years as $year) {
- echo '<option value="', $year, '"', (int) $meta_year == (int) $year ? ' selected="selected"' : '', '>', $year, '</option>';
- }
- echo '</select>';
- break;
- case 'taxonomy-select':
- $taxterms = get_terms($field['id'], 'hide_empty=0');
- echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
- $names = wp_get_object_terms($post->ID, $field['id']);
- echo '<option value=""';
- if (!count($names))
- echo 'selected';
- echo 'None</option>';
- foreach ($taxterms as $taxterm) {
- if (!is_wp_error($names) && !empty($names) && !strcmp($taxterm->slug, $names[0]->slug))
- echo '<option value="' . $taxterm->slug . '" selected>' . $taxterm->name . '</option>';
- else
- echo '<option value="' . $taxterm->slug . '">' . $taxterm->name . '</option>';
- }
- echo '</select>';
- break;
- case 'taxonomy-checkbox':
- $taxterms = get_terms($field['id'], 'hide_empty=0');
- $names = wp_get_object_terms($post->ID, $field['id']);
- foreach ($taxterms as $taxterm) {
- echo '<input type="checkbox" name="', $field['id'], '[]" id="', $field['id'], '[]"value="' . $taxterm->slug . '"';
- if(!is_wp_error($names) && !empty($names)) {
- foreach($names as $name) {
- if(!strcmp($taxterm->slug, $name->slug)) {
- echo 'checked="checked"';
- continue;
- }
- }
- }
- echo '>' . $taxterm->name . '</input>'.'<br />';
- }
- break;
- case 'radio':
- foreach ($field['options'] as $option) {
- echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
- }
- break;
- case 'checkbox':
- echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
- break;
- case 'file':
- echo $meta ? "$meta<br />" : '', '<input type="file" name="', $field['id'], '" id="', $field['id'], '" />','<br />', $field['desc'];
- break;
- case 'wysiwyg':
- echo '<textarea name="', $field['id'], '" id="', $field['id'], '" class="theEditor" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>','<br />', $field['desc'];
- break;
- case 'image':
- ?>
- <h2>Images attached to this post</h2>
- <div id="uploaded">
- <?php
- $args = array(
- 'post_type' => 'attachment',
- 'post_parent' => $post->ID,
- 'numberposts' => -1,
- 'post_status' => NULL
- );
- $attachs = get_posts($args);
- if (!empty($attachs)) {
- foreach ($attachs as $att) {
- ?>
- <div class="single-att" style="margin: 0 10px 10px 0; float: left;"> <?php echo wp_get_attachment_image($att->ID, 'thumbnail'); ?> <br />
- <a class="deletefile" href="#" rel="<?php echo $att->ID ?>-<?php echo $post_id ?> "title="Delete this file">Delete Image</a> </div>
- <?php
- }
- } else {
- echo 'No Images uploaded yet';
- }
- ?>
- </div>
- <h2>Upload new Images</h2>
- <div id="newimages">
- <p>
- <input type="file" name="<?php echo $field['id'] ?>[]" id="" />
- </p>
- <a class="add" href="#">Add More Images</a> </div>
- <?php
- break;
- }
- echo '<td>',
- '</tr>';
- }
- echo '</table>';
- }
- // Save data from meta box
- function save($post_id) {
- $real_post_id = isset($_POST['post_ID']) ? $_POST['post_ID'] : NULL ;
- // 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($real_post_id, $name, true);
- $new = $_POST[$field['id']];
- 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, $real_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);
- }
- if($field['type'] == 'date') {
- $name_m = $field['id'].'_month';
- $name_d = $field['id'].'_day';
- $name_y = $field['id'].'_year';
- $old_m = get_post_meta($real_post_id, $name_m, true);
- $old_d = get_post_meta($real_post_id, $name_d, true);
- $old_y = get_post_meta($real_post_id, $name_y, true);
- $new_m = $_POST[$name_m];
- $new_d = $_POST[$name_d];
- $new_y = $_POST[$name_y];
- }
- // 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($field['type'] == 'date') {
- if ($new_m && $new_m != $old_m) {
- update_post_meta($real_post_id, $name_m, $new_m);
- }elseif ('' == $new_m && $old_m ) {
- delete_post_meta($real_post_id, $name_d, $old_m);
- }
- if ($new_d && $new_d != $old_d) {
- update_post_meta($real_post_id, $name_d, $new_d);
- }elseif ('' == $new_d && $old_d ) {
- delete_post_meta($real_post_id, $name_d, $old_d);
- }
- if ($new_y && $new_y != $old_y) {
- update_post_meta($real_post_id, $name_y, $new_y);
- }elseif ('' == $new_y && $old_y ) {
- delete_post_meta($real_post_id, $name_y, $old_y);
- }
- } elseif ( $field['type'] == 'taxonomy-select' || $field['type'] == 'taxonomy-checkbox' ) {
- wp_set_object_terms( $real_post_id, $new, $name );
- } elseif ($new && $new != $old) {
- update_post_meta($real_post_id, $name, $new);
- } elseif ('' == $new && $old && $field['type'] != 'file' && $field['type'] != 'image') {
- delete_post_meta($real_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]);
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement