Advertisement
Guest User

Slide Metaboxes with TinyMCE & Image Dropdown attempt

a guest
Sep 22nd, 2010
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.75 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * Modified version of this: http://loak.pastebin.com/Mqb3pKhx (from http://chrisburbridge.com/?attachment_id=316)
  5. * Modification to allow for incremental versions of the same metabox, include multiple TinyMCE fields, and to have an Image Dropdown field for all the attached images.
  6. */
  7.  
  8. /* Set up some default variables. */
  9. $meta_boxes = array();
  10. $i = 0;
  11. $number_of_boxes = 15;
  12.  
  13. /* Loop through the boxes to create as many as you want. */
  14. while ( ++$i <= $number_of_boxes ) {
  15.     $prefix = "slide{$i}-";
  16.  
  17.     $meta_boxes[] = array(
  18.         'id' => "my-meta-box-{$i}",
  19.         'title' => sprintf( 'Slide %s', $i ),
  20.         'pages' => array( 'lom_slideshow','' ), // multiple post types
  21.         'context' => 'normal',
  22.         'priority' => 'high',
  23.         'fields' => array(
  24.             array( 'name' => 'Slide Title', 'desc' => 'Enter the title of the slide here', 'id' => $prefix . 'title',   'type' => 'text' ),
  25.             //array( 'name' => 'Slide Description', 'desc' => 'Provide a brief description of the picture or video',    'id' => $prefix . 'textarea', 'type' => 'textarea' ), //plain text description
  26.             array( 'name' => 'Slide Description', 'desc' => 'Provide a brief description of the picture or video',  'id' => $prefix . 'tinymce', 'type' => 'tinymce' ),
  27.             array( 'name' => 'Slide Image', 'desc' => 'Choose the image',   'id' => $prefix . 'image', 'type' => 'select' ),
  28.             //array( 'name' => 'Slide Image', 'desc' => 'Enter the URL of the image here',  'id' => $prefix . 'image', 'type' => 'text' ),
  29.             //array( 'name' => 'Image upload', 'desc' => 'Upload an Image',     'id' => $prefix . 'img', 'type' => 'image' ),
  30.             array( 'name' => 'Slide Embed Code', 'desc' => 'Insert embed code for the video', 'id' => $prefix .  'embed', 'type' => 'textarea' ),
  31.             array( 'name' => 'Hide Slide', 'id' => $prefix . 'checkbox', 'type' => 'checkbox' )
  32.         )
  33.     );
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. /*********************************
  42. Setup Class
  43. *********************************/
  44.  
  45. foreach ($meta_boxes as $meta_box) {
  46.     $my_box = new My_meta_box($meta_box);
  47. }
  48.  
  49. class My_meta_box {
  50.  
  51.     protected $_meta_box;
  52.  
  53.     // create meta box based on given data
  54.     function __construct($meta_box) {
  55.         if (!is_admin()) return;
  56.    
  57.         $this->_meta_box = $meta_box;
  58.  
  59.         // fix upload bug: http://www.hashbangcode.com/blog/add-enctype-wordpress-post-and-page-forms-471.html
  60.         $current_page = substr(strrchr($_SERVER['PHP_SELF'], '/'), 1, -4);
  61.         if ($current_page == 'page' || $current_page == 'page-new' || $current_page == 'post' || $current_page == 'post-new') {
  62.             add_action('admin_head', array(&$this, 'add_post_enctype'));
  63.         }
  64.        
  65.         add_action('admin_menu', array(&$this, 'add'));
  66.  
  67.         add_action('save_post', array(&$this, 'save'));
  68.     }
  69.    
  70.     function add_post_enctype() {
  71.         echo '
  72.         <script type="text/javascript">
  73.         jQuery(document).ready(function(){
  74.             jQuery("#post").attr("enctype", "multipart/form-data");
  75.             jQuery("#post").attr("encoding", "multipart/form-data");
  76.         });
  77.         </script>';
  78.     }
  79.    
  80.  
  81.  
  82.     /// Add meta box for multiple post types
  83.     function add() {
  84.         foreach ($this->_meta_box['pages'] as $page) {
  85.             add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
  86.         }
  87.     }
  88.  
  89.     // Callback function to show fields in meta box
  90.     function show() {
  91.         global $post;
  92.  
  93.         // I added this here to setup the Attachments for the dropdown
  94.         $images = array();
  95.         $attachments = get_posts( array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => null, 'post_status' => 'inherit', 'post_parent' => $post->ID ) );
  96.                    
  97.  
  98.  
  99.         // Use nonce for verification
  100.         echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  101.    
  102.         echo '<table class="form-table">';
  103.  
  104.         foreach ($this->_meta_box['fields'] as $field) {
  105.             // get current post meta data
  106.             $meta = get_post_meta($post->ID, $field['id'], true);
  107.        
  108.             echo '<tr>',
  109.                     '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  110.                     '<td>';
  111.             switch ($field['type']) {
  112.                 case 'text':
  113.                     echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
  114.                         '<br />', $field['desc'];
  115.                     break;
  116.                 case 'textarea':
  117.                     echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
  118.                         '<br />', $field['desc'];
  119.                     break;
  120.                 case 'tinymce':  
  121.                    array_push($mce_ids, $field['id']);
  122.                     echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
  123.                         '<br />';
  124.                     echo $field['desc'];
  125.                     break;
  126.                 // This is my attempt to get to get the Attachments to populate the <option> fields of the dropdown
  127.                 case 'select':
  128.                     if ( !empty( $attachments ) ) {
  129.                         foreach ($attachments as $attachment)
  130.                     $images[$attachment->ID] = wp_get_attachment_url( $attachment->ID );
  131.                    
  132.                         echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  133.                         foreach ( $field['options'] as $id => $url ) {
  134.                             echo '<option value="' . $id . '"' . selected( $id, $meta, false ) . '>' . $url . '</option>';
  135.                         }
  136.                     }
  137.  
  138.                 case 'radio':
  139.                     foreach ($field['options'] as $option) {
  140.                         echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  141.                     }
  142.                     break;
  143.                 case 'checkbox':
  144.                     echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  145.                     break;
  146.                 case 'file':
  147.                     echo $meta ? "$meta<br />" : '', '<input type="file" name="', $field['id'], '" id="', $field['id'], '" />',
  148.                         '<br />', $field['desc'];
  149.                     break;
  150.                 case 'image':
  151.                     echo $meta ? "<img src=\"$meta\" width=\"150\" height=\"150\" /><br />$meta<br />" : '', '<input type="file" name="', $field['id'], '" id="', $field['id'], '" />',
  152.                         '<br />', $field['desc'];
  153.                     break;
  154.             }
  155.             echo    '<td>',
  156.                 '</tr>';
  157.         }
  158.    
  159.         echo '</table>';
  160.        
  161.         // If we have accumulated any TinyMCE type items, then active TinyMCE on them
  162.         if (count($mce_ids) > 0) {
  163.             ?>
  164.                 <script type="text/javascript">
  165.                 /* <![CDATA[ */
  166.  
  167.                 jQuery(document).ready( function () {
  168.                     if ( typeof( tinyMCE ) == "object" && typeof( tinyMCE.execCommand ) == "function" ) {
  169.                         <?php foreach ($mce_ids as $id) : ?>
  170.                         tinyMCE.execCommand("mceAddControl", false, "<?php echo $id; ?>");    
  171.                         <?php endforeach; ?>
  172.                     }
  173.                 });
  174.  
  175.                 /* ]]> */
  176.                 </script>
  177.             <?php      
  178.         }
  179.        
  180.     }
  181.  
  182.     // Save data from meta box
  183.     function save($post_id) {
  184.         // verify nonce
  185.         if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
  186.             return $post_id;
  187.         }
  188.  
  189.         // check autosave
  190.         if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  191.             return $post_id;
  192.         }
  193.  
  194.         // check permissions
  195.         if ('page' == $_POST['post_type']) {
  196.             if (!current_user_can('edit_page', $post_id)) {
  197.                 return $post_id;
  198.             }
  199.         } elseif (!current_user_can('edit_post', $post_id)) {
  200.             return $post_id;
  201.         }
  202.  
  203.         foreach ($this->_meta_box['fields'] as $field) {
  204.             $name = $field['id'];
  205.            
  206.             $old = get_post_meta($post_id, $name, true);
  207.             $new = $_POST[$field['id']];
  208.            
  209.             if ($field['type'] == 'file' || $field['type'] == 'image') {
  210.                 $file = wp_handle_upload($_FILES[$name], array('test_form' => false));
  211.                 $new = $file['url'];
  212.             }
  213.            
  214.             if ($new && $new != $old) {
  215.                 update_post_meta($post_id, $name, $new);
  216.             } elseif ('' == $new && $old && $field['type'] != 'file' && $field['type'] != 'image') {
  217.                 delete_post_meta($post_id, $name, $old);
  218.             }
  219.         }
  220.     }
  221. }
  222. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement