Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //-------------------------------------------------------------------
- // custom meta boxes
- //-------------------------------------------------------------------
- //-------------------------------------
- function add_custom_meta_box() {
- //-------------------------------------
- add_meta_box(
- 'custom_meta_box', // $id
- 'Custom Meta Box', // $title
- //----------------------------------
- 'show_custom_meta_box',
- //----------------------------------
- 'post', // $page
- 'normal', // $context
- 'high'); // $priority
- }
- //-------------------------------------
- add_action('add_meta_boxes', 'add_custom_meta_box');
- $prefix = 'custom_';
- //-------------------------------------
- $custom_meta_fields = array(
- //-------------------------------------
- array(
- 'label' => 'Text Input',
- 'desc' => 'A description for the field.',
- 'id' => $prefix.'text',
- 'type' => 'text'
- ),
- array(
- 'label' => 'Textarea',
- 'desc' => 'A description for the field.',
- 'id' => $prefix.'textarea',
- 'type' => 'textarea'
- ),
- array(
- 'label' => 'Checkbox Input',
- 'desc' => 'A description for the field.',
- 'id' => $prefix.'checkbox',
- 'type' => 'checkbox'
- ),
- //-------------------------------------
- array(
- 'label' => 'Select Box',
- 'desc' => 'A description for the field.',
- 'id' => $prefix.'select',
- 'type' => 'select',
- 'options' => array (
- 'one' => array (
- 'label' => 'Option One',
- 'value' => 'one'
- ),
- 'two' => array (
- 'label' => 'Option Two',
- 'value' => 'two'
- ),
- 'three' => array (
- 'label' => 'Option Three',
- 'value' => 'three'
- )
- )
- ),
- //-------------------------------------
- array (
- 'label' => 'Radio Group',
- 'desc' => 'A description for the field.',
- 'id' => $prefix.'radio',
- 'type' => 'radio',
- 'options' => array (
- 'one' => array (
- 'label' => 'Option One',
- 'value' => 'one'
- ),
- 'two' => array (
- 'label' => 'Option Two',
- 'value' => 'two'
- ),
- 'three' => array (
- 'label' => 'Option Three',
- 'value' => 'three'
- )
- )
- ),
- //-------------------------------------
- array (
- 'label' => 'Checkbox Group',
- 'desc' => 'A description for the field.',
- 'id' => $prefix.'checkbox_group',
- 'type' => 'checkbox_group',
- 'options' => array (
- 'one' => array (
- 'label' => 'Option One',
- 'value' => 'one'
- ),
- 'two' => array (
- 'label' => 'Option Two',
- 'value' => 'two'
- ),
- 'three' => array (
- 'label' => 'Option Three',
- 'value' => 'three'
- )
- )
- ),
- //-------------------------------------
- array(
- 'label' => 'Category',
- 'id' => 'category',
- 'type' => 'tax_select'
- ),
- array(
- 'label' => 'Post List',
- 'desc' => 'A description for the field.',
- 'id' => $prefix.'post_id',
- 'type' => 'post_list',
- 'post_type' => array('post','page')
- ),
- array(
- 'label' => 'Date',
- 'desc' => 'A description for the field.',
- 'id' => $prefix.'date',
- 'type' => 'date'
- ),
- array(
- 'label' => 'Slider',
- 'desc' => 'A description for the field.',
- 'id' => $prefix.'slider',
- 'type' => 'slider',
- 'min' => '0',
- 'max' => '100',
- 'step' => '5'
- ),
- array(
- 'label' => 'Image',
- 'desc' => 'A description for the field.',
- 'id' => $prefix.'image',
- 'type' => 'image'
- ),
- array(
- 'label' => 'Repeatable',
- 'desc' => 'A description for the field.',
- 'id' => $prefix.'repeatable',
- 'type' => 'repeatable'
- )
- );
- //-------------------------------------
- if(is_admin()) {
- wp_enqueue_script('jquery-ui-datepicker');
- wp_enqueue_script('jquery-ui-slider');
- wp_enqueue_script('custom-js',
- get_template_directory_uri().'/js/custom-js.js');
- wp_enqueue_style('jquery-ui-custom',
- get_template_directory_uri().'/css/jquery-ui-custom.css');
- }
- //-------------------------------------------------
- add_action('admin_head','add_custom_scripts');
- //-------------------------------------------------
- function add_custom_scripts() {
- //-------------------------------------------------
- global $custom_meta_fields, $post;
- $output = '<script type="text/javascript">
- jQuery(function() {';
- //-----------------------------------------
- foreach ($custom_meta_fields as $field) {
- //---------------------------------
- if($field['type'] == 'date')
- $output .= 'jQuery(".datepicker").datepicker();';
- //---------------------------------
- if ($field['type'] == 'slider') {
- $value = get_post_meta($post->ID, $field['id'], true);
- if ($value == '') $value = $field['min'];
- $output .= '
- jQuery( "#'.$field['id'].'-slider" ).slider({
- value: '.$value.',
- min: '.$field['min'].',
- max: '.$field['max'].',
- step: '.$field['step'].',
- slide: function( event, ui ) {
- jQuery( "#'.$field['id'].'" ).val( ui.value );
- }});';
- }
- }
- $output .= '});
- </script>';
- echo $output;
- }
- //-------------------------------------------------
- function show_custom_meta_box() {
- //-------------------------------------------------
- global $custom_meta_fields, $post;
- echo '<input type="hidden" name="custom_meta_box_nonce" value="'.
- wp_create_nonce(basename(__FILE__)).'" />';
- echo '<table class="form-table">';
- //-------------------------------------------
- foreach ($custom_meta_fields as $field) {
- //-------------------------------------------
- $meta = get_post_meta($post->ID, $field['id'], true);
- echo '<tr><th><label for="'.$field['id'].'">'.$field['label'].'</label></th><td>';
- switch($field['type']) {
- //-------------------
- case 'text':
- //-------------------
- echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].
- '" value="'.$meta.'" size="30" /><br /><span class="description">'.
- $field['desc'].'</span>';
- break;
- //---------------------
- case 'textarea':
- //---------------------
- echo '<textarea name="'.$field['id'].'" id="'.$field['id'].
- '" cols="60" rows="4">'.$meta.'</textarea><br />
- <span class="description">'.$field['desc'].'</span>';
- break;
- //---------------------
- case 'checkbox':
- //---------------------
- echo '<input type="checkbox" name="'.$field['id'].'" id="'.
- $field['id'].'" ',$meta ? ' checked="checked"' : '','/>
- <label for="'.$field['id'].'">'.$field['desc'].'</label>';
- break;
- //---------------------
- case 'select':
- //---------------------
- echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
- foreach ($field['options'] as $option) {
- echo '<option', $meta == $option['value'] ?
- ' selected="selected"' : '',
- ' value="'.$option['value'].'">'.$option['label'].'</option>';
- }
- echo '</select><br /><span class="description">'.
- $field['desc'].'</span>';
- break;
- //---------------------
- case 'radio':
- //---------------------
- foreach ( $field['options'] as $option ) {
- echo '<input type="radio" name="'.$field['id'].
- '" id="'.$option['value'].'" value="'.$option['value'].
- '" ',$meta == $option['value'] ? ' checked="checked"' : '',' />
- <label for="'.$option['value'].'">'.$option['label'].'</label><br />';
- }
- echo '<span class="description">'.$field['desc'].'</span>';
- break;
- //----------------------
- case 'checkbox_group':
- //----------------------
- foreach ($field['options'] as $option) {
- echo '<input type="checkbox" value="'.$option['value'].
- '" name="'.$field['id'].'[]" id="'.$option['value'].'"',
- $meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' />
- <label for="'.$option['value'].'">'.$option['label'].'</label><br />';
- }
- echo '<span class="description">'.$field['desc'].'</span>';
- break;
- //----------------------
- case 'tax_select':
- //----------------------
- echo '<select name="'.$field['id'].'" id="'.$field['id'].'">
- <option value="">Select One</option>';
- $terms = get_terms($field['id'], 'get=all');
- $selected = wp_get_object_terms($post->ID, $field['id']);
- foreach ($terms as $term) {
- if (!empty($selected) && !strcmp($term->slug, $selected[0]->slug))
- echo '<option value="'.$term->slug.'" selected="selected">'.
- $term->name.'</option>';
- else
- echo '<option value="'.$term->slug.'">'.$term->name.'</option>';
- }
- $taxonomy = get_taxonomy($field['id']);
- echo '</select><br /><span class="description"><a href="'.
- get_bloginfo('home').
- '/wp-admin/edit-tags.php?taxonomy='.$field['id'].
- '">Manage '.$taxonomy->label.'</a></span>';
- break;
- //----------------------
- case 'post_list':
- //----------------------
- $items = get_posts( array (
- 'post_type' => $field['post_type'],
- 'posts_per_page' => -1
- ));
- echo '<select name="'.$field['id'].'" id="'.$field['id'].'">
- <option value="">Select One</option>';
- foreach($items as $item) {
- echo '<option value="'.$item->ID.
- '"',$meta == $item->ID ? ' selected="selected"' : '','>'.
- $item->post_type.': '.$item->post_title.'</option>';
- }
- echo '</select><br /><span class="description">'.$field['desc'].'</span>';
- break;
- //----------------------
- case 'date':
- //----------------------
- echo '<input type="text" class="datepicker" name="'.$field['id'].
- '" id="'.$field['id'].'" value="'.$meta.'" size="30" />
- <br /><span class="description">'.$field['desc'].'</span>';
- break;
- //----------------------
- case 'slider':
- //----------------------
- $value = $meta != '' ? $meta : '0';
- echo '<div id="'.$field['id'].'-slider"></div>
- <input type="text" name="'.$field['id'].'" id="'.$field['id'].
- '" value="'.$value.'" size="5" />
- <br /><span class="description">'.$field['desc'].'</span>';
- break;
- //----------------------
- case 'image':
- //----------------------
- $image = get_template_directory_uri().'/images/image.png';
- echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
- if ($meta) {
- $image = wp_get_attachment_image_src($meta, 'medium');
- $image = $image[0];
- }
- echo '<input name="'.$field['id'].
- '" type="hidden" class="custom_upload_image" value="'.$meta.'" />
- <img src="'.$image.'" class="custom_preview_image" alt="" /><br />
- <input class="custom_upload_image_button button"
- type="button" value="Choose Image" />
- <small> <a href="#" class="custom_clear_image_button">
- Remove Image</a></small>
- <br clear="all" /><span class="description">'.$field['desc'].'</span>';
- break;
- //----------------------
- case 'repeatable':
- //----------------------
- echo '<a class="repeatable-add button" href="#">+</a>
- <ul id="'.$field['id'].'-repeatable" class="custom_repeatable">';
- $i = 0;
- if ($meta) {
- foreach($meta as $row) {
- echo '<li><span class="sort hndle">|||</span>
- <input type="text" name="'.
- $field['id'].'['.$i.']" id="'.$field['id'].
- '" value="'.$row.'" size="30" />
- <a class="repeatable-remove button" href="#">-</a></li>';
- $i++;
- }} else {
- echo '<li><span class="sort hndle">|||</span>
- <input type="text" name="'.
- $field['id'].'['.$i.']" id="'.
- $field['id'].'" value="" size="30" />
- <a class="repeatable-remove button" href="#">-</a></li>';
- }
- echo '</ul>
- <span class="description">'.$field['desc'].'</span>';
- break;
- }
- echo '</td></tr>';
- }
- echo '</table>';
- }
- //-----------------------------------------
- function remove_taxonomy_boxes() {
- remove_meta_box('categorydiv', 'post', 'side');
- }
- //-----------------------------------------
- add_action( 'admin_menu' , 'remove_taxonomy_boxes' );
- //-----------------------------------------
- function save_custom_meta($post_id) {
- //-----------------------------------------
- global $custom_meta_fields;
- if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
- return $post_id;
- if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
- return $post_id;
- 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 ($custom_meta_fields as $field) {
- //-----------------------------------------
- if($field['type'] == 'tax_select') continue;
- $old = get_post_meta($post_id, $field['id'], true);
- $new = $_POST[$field['id']];
- if ($new && $new != $old) {
- update_post_meta($post_id, $field['id'], $new);
- } elseif ('' == $new && $old) {
- delete_post_meta($post_id, $field['id'], $old);
- }
- }
- // save taxonomies
- //-----------------------------------------
- $post = get_post($post_id);
- $category = $_POST['category'];
- wp_set_object_terms( $post_id, $category, 'category' );
- }
- add_action('save_post', 'save_custom_meta');
- ?>
- jQuery(function(jQuery) {
- jQuery('#media-items').bind('DOMNodeInserted',function(){
- jQuery('input[value="Insert into Post"]').each(function(){
- jQuery(this).attr('value','Use This Image');
- });
- });
- jQuery('.custom_upload_image_button').click(
- function() {
- formfield = jQuery(this).siblings('.custom_upload_image');
- preview = jQuery(this).siblings('.custom_preview_image');
- tb_show('', 'media-upload.php?type=image&TB_iframe=true');
- window.send_to_editor = function(html) {
- imgurl = jQuery('img',html).attr('src');
- classes = jQuery('img', html).attr('class');
- id = classes.replace(/(.*?)wp-image-/, '');
- formfield.val(id);
- preview.attr('src', imgurl);
- tb_remove();
- }
- return false;
- });
- jQuery('.custom_clear_image_button').click(
- function() {
- var defaultImage = jQuery(this).parent().siblings('.custom_default_image').text();
- jQuery(this).parent().siblings('.custom_upload_image').val('');
- jQuery(this).parent().siblings('.custom_preview_image').attr('src', defaultImage);
- return false;
- });
- jQuery('.repeatable-add').click(
- function() {
- field = jQuery(this).closest('td').find('.custom_repeatable li:last').clone(true);
- fieldLocation = jQuery(this).closest('td').find('.custom_repeatable li:last');
- jQuery('input', field).val('').attr('name', function(index, name) {
- return name.replace(/(\d+)/, function(fullMatch, n) {
- return Number(n) + 1;
- });
- })
- field.insertAfter(fieldLocation, jQuery(this).closest('td'))
- return false;
- });
- jQuery('.repeatable-remove').click(
- function(){
- jQuery(this).parent().remove();
- return false;
- });
- jQuery('.custom_repeatable').sortable({
- opacity: 0.6,
- revert: true,
- cursor: 'move',
- handle: '.sort'
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement