Advertisement
retesere20

Untitled

Sep 1st, 2016
4,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. // ============== add as many multiple fields as you want ===============//
  2. Add_Media_Uploader_X('my-page-logo');
  3. Add_Media_Uploader_X('my-sticker-image');
  4. Add_Media_Uploader_X('post-poopup-picture');
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. // ============== but at first define this function =====================//
  12.  
  13. function Add_Media_Uploader_X( $title){
  14. $GLOBALS['image_uploader_fields'][] = $title;
  15.  
  16. // avoid the below block to execute multiple times
  17. if (!defined('already_triggered_999999')){
  18. define('already_triggered_999999',true);
  19. Register_uploader_functionality();
  20. }
  21. }
  22.  
  23. function Register_uploader_functionality(){
  24.  
  25. add_action('plugins_loaded', function(){
  26. if($GLOBALS['pagenow']=='post.php'){
  27. add_action('admin_print_scripts', 'my_admin_scripts');
  28. add_action('admin_print_styles', 'my_admin_styles');
  29. }
  30. });
  31. function my_admin_scripts() {wp_enqueue_script('jquery'); wp_enqueue_script('media-upload'); wp_enqueue_script('thickbox'); }
  32. function my_admin_styles() {wp_enqueue_style('thickbox'); }
  33. add_action('add_meta_boxes', function(){ add_meta_box('my-Images-Upload', 'my-Images-Upload-box','myfunc8888', get_post_types(),'normal'); }, 9);
  34.  
  35.  
  36. function myfunc8888($post){
  37. foreach ( $GLOBALS['image_uploader_fields'] as $each) {
  38. $url = get_post_meta($post->ID,'my-image--'.$each, true);
  39. ?>
  40. <div class="<?php echo $each;?>" style="border:1px solid black; border-width:0 0 2px 0;">
  41. <h1><?php echo $each;?></h1>
  42. <input id="my_upl_button_<?php echo $each;?>" type="button" value="Upload Image" /> <input id="my_image_URL_<?php echo $each;?>" name="my_image_URL_<?php echo $each;?>" type="text" value="<?php echo $url;?>" style="width:400px;" />
  43. <br/><img src="<?php echo $url;?>" style="width:200px;" id="picsrc_<?php echo $each;?>" />
  44. <script>
  45. jQuery(document).ready( function( $ ) {
  46. jQuery('#my_upl_button_<?php echo $each;?>').click(function() {
  47. //use here, because you may have multiple buttons, so `send_to_editor` needs fresh
  48. window.send_to_editor = function(html) {
  49. imgurl = jQuery(html).attr('src')
  50. jQuery('#my_image_URL_<?php echo $each;?>').val(imgurl);
  51. jQuery('#picsrc_<?php echo $each;?>').attr("src",imgurl);
  52. tb_remove();
  53. }
  54.  
  55. formfield = jQuery('#my_image_URL_<?php echo $each;?>').attr('name');
  56. tb_show( '', 'media-upload.php?type=image&amp;TB_iframe=true' );
  57. return false;
  58. });
  59. });
  60. </script>
  61. </div>
  62. <?php
  63.  
  64. }
  65.  
  66. }
  67.  
  68.  
  69. add_action( 'save_post', function ($post_id) {
  70. foreach ( $GLOBALS['image_uploader_fields'] as $each) {
  71. if (isset($_POST['my_image_URL_'.$each])){
  72. update_post_meta($post_id, 'my-image--'.$each, $_POST['my_image_URL_'.$each]);
  73. }
  74. }
  75. });
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement