retesere20

meta-boxes-metaboxes-media-uploader

Feb 25th, 2020
106
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public function Media_Uploader_code($value, $each_title){
  2. ?>
  3. <div class="<?php echo $each_title;?>" style="border:1px gray black; border-width:0 0 2px 0;">
  4. <h1><?php echo $each_title;?></h1>
  5. <input id="my_upl_button_<?php echo $each_title;?>" type="button" value="Upload Image" /> <input id="my_image_URL_<?php echo $each_title;?>" name="my_metabxs[myX_<?php echo $each_title;?>]" type="text" value="<?php echo $value;?>" style="width:400px;" />
  6. <br/><img src="<?php echo $value;?>" style="width:160px;<?php if (empty($value)) {echo "display:none;";} ?>" id="picsrc_<?php echo $each_title;?>" />
  7. <script>
  8. jQuery(document).ready( function( $ ) {
  9.  
  10. jQuery('#my_upl_button_<?php echo $each_title;?>').click(function() {
  11. //use here, because you may have multiple buttons, so `send_to_editor` needs fresh
  12. window.send_to_editor = function(html) {
  13. imgurl = jQuery(html).attr('src')
  14. jQuery('#my_image_URL_<?php echo $each_title;?>').val(imgurl);
  15. jQuery('#picsrc_<?php echo $each_title;?>').attr("src", imgurl).css("display","inline-block");
  16. tb_remove();
  17. }
  18.  
  19. formfield = jQuery('#my_image_URL_<?php echo $each_title;?>').attr('name');
  20. tb_show( '', 'media-upload.php?type=image&amp;TB_iframe=true' );
  21. return false;
  22. });
  23.  
  24. });
  25. </script>
  26. </div>
  27. <?php
  28. }
  29.  
  30.  
  31. public function my_metabox( $type='text', $title){
  32. $GLOBALS['fields_for___'.$type][] = $title;
  33.  
  34. // avoid the below block to execute multiple times
  35. if (!defined('triggered_999999_'.$type)){ define('triggered_999999_'.$type,true);
  36. Register_functionality_for($type) ;
  37. }
  38. }
  39.  
  40. public function Register_functionality_for($type){
  41.  
  42. if ($type=='media_uploader'){
  43.  
  44. add_action('plugins_loaded', function(){
  45. if($GLOBALS['pagenow']=='post.php'){
  46. add_action('admin_print_scripts', function() {wp_enqueue_script('jquery'); wp_enqueue_script('media-upload'); wp_enqueue_script('thickbox'); } );
  47. add_action('admin_print_styles', function() {wp_enqueue_style('thickbox'); });
  48. }
  49. });
  50.  
  51.  
  52.  
  53. add_action('add_meta_boxes', function(){ add_meta_box('my-Images-Upload', 'my-Images-Upload-box','myfunc8888', get_post_types(),'normal'); }, 9);
  54. public function myfunc8888($post){
  55. foreach ( $GLOBALS['fields_for___media_uploader'] as $each) {
  56. $value = get_post_meta($post->ID, $each, true);
  57. Media_Uploader_code($value, $each);
  58. }
  59. }
  60.  
  61. }
  62.  
  63. elseif($type=='text'){
  64. add_action('add_meta_boxes', function(){ add_meta_box('my-fieldss', 'my-text-boxes','myfunc33322', get_post_types(),'normal'); }, 9);
  65. public function myfunc33322($post){
  66. foreach ( $GLOBALS['fields_for___text'] as $each) {
  67. $value = get_post_meta($post->ID, $each, true);
  68. ?>
  69. <div class="<?php echo $each;?>" style="border:1px solid black; border-width:0 0 2px 0;">
  70. <h1><?php echo $each;?></h1>
  71. <input id="my_field_<?php echo $each;?>" name="my_metabxs[myX_<?php echo $each;?>]" type="text" value="<?php echo $value;?>" style="width:400px;" />
  72. </div>
  73. <?php
  74.  
  75. }
  76. }
  77. }
  78.  
  79. elseif($type=='checkbox'){
  80. add_action('add_meta_boxes', function(){ add_meta_box('my-checkboxs', 'my-checkboxes','myfunc6555', get_post_types(),'normal'); }, 9);
  81. public function myfunc6555($post){
  82. foreach ( $GLOBALS['fields_for___checkbox'] as $each) {
  83. $value = get_post_meta($post->ID, $each, true);
  84. ?>
  85. <div class="<?php echo $each;?>" style="border:1px solid black; border-width:0 0 2px 0;">
  86. <h1><?php echo $each;?></h1>
  87. <input type="hidden" name="my_metabxs[myX_<?php echo $each;?>]" value="0" />
  88. <input type="checkbox" name="my_metabxs[myX_<?php echo $each;?>]" value="1" <?php echo ($value? 'checked="checked"': '');?> />
  89. </div>
  90. <?php
  91.  
  92. }
  93. }
  94. }
  95.  
  96. elseif($type=='textarea_FULL'){
  97. add_action('add_meta_boxes', function(){ add_meta_box('my-textarea-fieldss', 'my-textarea1-boxes','myfunc6444', get_post_types(),'normal'); }, 9);
  98. public function myfunc6444($post){
  99. foreach ( $GLOBALS['fields_for___textarea_FULL'] as $each) {
  100. $value = get_post_meta($post->ID, $each, true);
  101. ?>
  102. <div class="<?php echo $each;?>" style="border:1px solid black; border-width:0 0 2px 0;">
  103. <h1><?php echo $each;?></h1>
  104. <?php wp_editor( htmlspecialchars_decode($value), 'styl_ID_'. $each, $settings = array('textarea_name'=>'my_metabxs[myX_'.$each.']', 'editor_class' => "txtaream editoor_ful"));?>
  105. </div>
  106. <?php
  107.  
  108. }
  109. }
  110. }
  111. elseif($type=='textarea_MINIMAL'){
  112. add_action('add_meta_boxes', function(){ add_meta_box('my-textarea-fieldss2', 'my-textarea2-boxes','myfunc88884', get_post_types(),'normal'); }, 9);
  113. public function myfunc88884($post){
  114. foreach ( $GLOBALS['fields_for___textarea_MINIMAL'] as $each) {
  115. $value = get_post_meta($post->ID, $each, true);
  116. ?>
  117. <div class="<?php echo $each;?>" style="border:1px solid black; border-width:0 0 2px 0;">
  118. <h1><?php echo $each;?></h1>
  119. <?php wp_editor( htmlspecialchars_decode($value), 'styl_ID_'. $each, $settings = array('textarea_name'=>'my_metabxs[myX_'.$each.']', 'teeny'=>true, 'tinymce'=>false, 'editor_class' => "txtaream editoor_min", 'media_buttons'=>false ));?>
  120. <style>.editoor_min{height:160px;}</style>
  121. </div>
  122. <?php
  123.  
  124. }
  125. }
  126. }
  127.  
  128.  
  129.  
  130. // Save Action
  131. add_action( 'save_post', public function ($post_id) {
  132. if (!empty($_POST['my_metabxs'])){
  133. foreach ($_POST['my_metabxs'] as $key=>$value) {
  134. update_post_meta($post_id, str_replace('myX_','', $key), $value);
  135. }
  136. }
  137. });
  138. }
RAW Paste Data