Advertisement
Guest User

dynamic uploader for wordpress

a guest
Nov 3rd, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.31 KB | None | 0 0
  1. /* TEXT UPLOADER FOR TEXT PAGES */
  2.  
  3. add_action( 'add_meta_boxes', 'add_custom_text_box' );
  4.  
  5. /* Do something with the data entered */
  6. add_action( 'save_post', 'save_text_postdata' );
  7.  
  8.  
  9. /* Adds a box to the main column on the Post and Page edit screens */
  10. function add_custom_text_box() {
  11.  
  12.         add_meta_box(
  13.         'dynamic_sectionid',
  14.         __( 'Text Contents', 'myplugin_textdomain' ),
  15.         'inner_text_custom_box',
  16.         'sc_text');
  17.    
  18. }
  19.  
  20. /* Prints the box content */
  21. function inner_text_custom_box() {
  22.     global $post;
  23.     // Use nonce for verification
  24.     wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
  25.     ?>
  26.     <div id="meta_inner">
  27.     <ul>
  28.    
  29.     <?php
  30.     //get the saved meta as an array
  31.     $images = get_post_meta($post->ID,'images',true);
  32.  
  33.     $c = 0;
  34.     if(is_array($images)){
  35.         foreach($images as $image ){
  36.             if (isset($image['url'])){
  37.                 echo '<li>
  38.                <span class="description">Link Text </span>
  39.                <input class="title" type="text" name="images['.$c.'][link_text]" value="'.$image['link_text'].'" /><br/>
  40.                
  41.                <span class="description">Link URL </span>
  42.                <input class="year" type="text" name="images['.$c.'][link_url]" value="'.$image['link_url'].'" /><br/>
  43.                
  44.                <span class="description">Subtext </span>
  45.                <input class="materials" type="text" name="images['.$c.'][subtext]" value="'.$image['subtext'].'" /><br/>
  46.                
  47.                <span class="description">Image </span>
  48.                <input class="upload" type="text" name="images['.$c.'][url]" value="'.$image['url'].'" />
  49.                
  50.                <input type="button" name="uploadbutton'.$c.'" id="uploadbutton'.$c.'" class="uploadbutton" value="insert image" />
  51.                
  52.                <img src="'.$image['url'].'" width="70" height="auto" />
  53.                
  54.                <span class="remove"><input type="button" name="remove" id="remove" value="remove" /></span>
  55.                
  56.                </li>';
  57.                 $c = $c +1;
  58.             }
  59.            
  60.             if ( $c == 1 )
  61.                     break;
  62.         }
  63.     }
  64.  
  65.     ?>
  66. <span id="here"></span>
  67. <span class="add"><?php echo __('<input type="button" name="addimage" id="addimage" value="add image" />'); ?></span>
  68. <script>
  69.     var $ =jQuery.noConflict();
  70.     $(document).ready(function() {
  71.         var count = <?php echo $c; ?>;
  72.        
  73.         if (count == 1){
  74.             $(".add").hide();
  75.         }
  76.        
  77.        
  78.         $(".add").click(function() {
  79.            
  80.             $(".add").hide();
  81.             count = count + 1;
  82.            
  83.             $('#here').append('<li><span class="description">Link Text </span><input class="title" type="text" name="images['+count+'][link_text]" value="" /><br/><span class="description">Link URL </span><input class="year" type="text" name="images['+count+'][link_url]" value="" /><br/><span class="description">Subtext </span><input class="materials" type="text" name="images['+count+'][subtext]" value="" /><br/><span class="description">Image </span><input class="upload" type="text" name="images['+count+'][url]" value="" /><input type="button" name="uploadbutton'+count+'" id="uploadbutton'+count+'" class="uploadbutton" value="insert image" /><img src="http://dummyimage.com/100x30/ddd/ddd" width="100" height="auto" /><span class="remove"><input type="button" name="remove" id="remove" value="remove" /></span></li>' );
  84.            
  85.             return false;
  86.         });
  87.         $(".remove").live('click', function() {
  88.             $(this).parent().remove();
  89.         });
  90.  
  91.         var uploadID = '';
  92.         var thumbsrc = '';
  93.        
  94.         jQuery('.uploadbutton').live('click', function() {
  95.             uploadID = jQuery(this).prev('input');
  96.             thumbsrc = jQuery(this).next('img');
  97.             formfield = jQuery('.upload').attr('name');
  98.            
  99.             tempsendtoeditor = window.send_to_editor; // store it for after
  100.            
  101.             window.send_to_editor = function(html) {
  102.                 imgurl = jQuery('img',html).attr('src');
  103.                 uploadID.val(imgurl);
  104.                 thumbsrc.attr('src', imgurl );
  105.                 tb_remove();
  106.                 window.send_to_editor = tempsendtoeditor; // restore it to original
  107.             };
  108.            
  109.             tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
  110.             return false;
  111.         });
  112.    
  113.        
  114.        
  115.     });
  116. </script>
  117.        
  118.         <script type="text/javascript">
  119.             jQuery(document).ready(function() {
  120.                 jQuery("#meta_inner").sortable({
  121.                     'tolerance':'intersect',
  122.                     'cursor':'pointer',
  123.                     'items':'li',
  124.                     'placeholder':'placeholder',
  125.                     'nested': 'ul'
  126.                 });
  127.             });
  128.         </script>
  129.  
  130.    </ul>
  131. </div>
  132.  
  133. <!-- /* STYLES */ -->
  134. <style type="text/css">
  135. #meta_inner ul { margin-left:0px; list-style: none; }
  136. #meta_inner li { padding: 2px 0px 2px 5px; margin: 4px 0px;  border: 1px solid #DDDDDD; cursor: move; -moz-border-radius:6px; clear: both; height: 100px; position: relative;}
  137. #meta_inner li span.description { float: left; padding: 5px 0 0 0; width: 60px;}
  138. #meta_inner input {margin-right: 5px;}
  139. #meta_inner input.upload, #meta_inner input.title, #meta_inner input.year, #meta_inner input.materials {width: 400px;}
  140. #meta_inner { list-style-type: none; margin: 10px 0 0 0; padding: 0; width: 100%; }
  141. #meta_inner li img {position: absolute; top: 5px; right: 5px;}
  142. #meta_inner li.placeholder{border: dashed 2px #ccc;background-color:#FFF;height:20px;}
  143. #meta_inner #remove {float: right;}
  144. </style>
  145.  
  146. <?php
  147.  
  148. }
  149.  
  150. /* When the post is saved, saves our custom data */
  151. function save_text_postdata( $post_id ) {
  152.     // verify if this is an auto save routine.
  153.     // If it is our form has not been submitted, so we dont want to do anything
  154.     if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  155.         return;
  156.     // verify this came from the our screen and with proper authorization,
  157.     // because save_post can be triggered at other times
  158.     if (isset($_POST['dynamicMeta_noncename'])){
  159.         if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], plugin_basename( __FILE__ ) ) )
  160.             return;
  161.     }else{return;}
  162.     // OK, we're authenticated: we need to find and save the data
  163.     $images = $_POST['images'];
  164.     update_post_meta($post_id,'images',$images);
  165. }
  166.  
  167. ?>
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement