Guest User

category-image

a guest
Sep 21st, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. <?php
  2. function wpst_load_wp_media_files()
  3. {
  4. wp_enqueue_media();
  5. }
  6. add_action('admin_enqueue_scripts', 'wpst_load_wp_media_files');
  7.  
  8. /**
  9. * Plugin class
  10. *
  11. */
  12. if (!class_exists('CT_TAX_META'))
  13. {
  14. class CT_TAX_META
  15. {
  16. public function __construct()
  17. {
  18. //
  19.  
  20. }
  21.  
  22. /*
  23. * Initialize the class and start calling our hooks and filters
  24. * @since 1.0.0
  25. */
  26. public function wpst_init()
  27. {
  28. add_action('category_add_form_fields', array(
  29. $this,
  30. 'wpst_add_category_image'
  31. ) , 10, 2);
  32. add_action('created_category', array(
  33. $this,
  34. 'wpst_save_category_image'
  35. ) , 10, 2);
  36. add_action('category_edit_form_fields', array(
  37. $this,
  38. 'wpst_update_category_image'
  39. ) , 10, 2);
  40. add_action('edited_category', array(
  41. $this,
  42. 'wpst_updated_category_image'
  43. ) , 10, 2);
  44. add_action('admin_footer', array(
  45. $this,
  46. 'wpst_add_script'
  47. ));
  48. }
  49.  
  50. /*
  51. * Add a form field in the new category page
  52. * @since 1.0.0
  53. */
  54. public function wpst_add_category_image($taxonomy)
  55. { ?>
  56. <div class="form-field term-group">
  57. <label for="category-image-id"><?php _e('Image', 'wpst'); ?></label>
  58. <input type="hidden" id="category-image-id" name="category-image-id" class="custom_media_url" value="">
  59. <div id="category-image-wrapper"></div>
  60. <p>
  61. <input type="button" class="button button-secondary ct_tax_media_button" id="ct_tax_media_button" name="ct_tax_media_button" value="<?php _e('Add Image', 'wpst'); ?>" />
  62. <input type="button" class="button button-secondary ct_tax_media_remove" id="ct_tax_media_remove" name="ct_tax_media_remove" value="<?php _e('Remove Image', 'wpst'); ?>" />
  63. </p>
  64. </div>
  65. <?php
  66. }
  67.  
  68. /*
  69. * Save the form field
  70. * @since 1.0.0
  71. */
  72. public function wpst_save_category_image($term_id, $tt_id)
  73. {
  74. if (isset($_POST['category-image-id']) && '' !== $_POST['category-image-id'])
  75. {
  76. $image = $_POST['category-image-id'];
  77. add_term_meta($term_id, 'category-image-id', $image, true);
  78. }
  79. }
  80.  
  81. /*
  82. * Edit the form field
  83. * @since 1.0.0
  84. */
  85. public function wpst_update_category_image($term, $taxonomy)
  86. { ?>
  87. <tr class="form-field term-group-wrap">
  88. <th scope="row">
  89. <label for="category-image-id"><?php _e('Image', 'wpst'); ?></label>
  90. </th>
  91. <td>
  92. <?php $image_id = get_term_meta($term->term_id, 'category-image-id', true); ?>
  93. <input type="hidden" id="category-image-id" name="category-image-id" value="<?php echo $image_id; ?>">
  94. <div id="category-image-wrapper">
  95. <?php if ($image_id)
  96. { ?>
  97. <?php echo wp_get_attachment_image($image_id, 'thumbnail'); ?>
  98. <?php
  99. } ?>
  100. </div>
  101. <p>
  102. <input type="button" class="button button-secondary ct_tax_media_button" id="ct_tax_media_button" name="ct_tax_media_button" value="<?php _e('Add Image', 'wpst'); ?>" />
  103. <input type="button" class="button button-secondary ct_tax_media_remove" id="ct_tax_media_remove" name="ct_tax_media_remove" value="<?php _e('Remove Image', 'wpst'); ?>" />
  104. </p>
  105. </td>
  106. </tr>
  107. <?php
  108. }
  109. /*
  110. * Update the form field value
  111. * @since 1.0.0
  112. */
  113. public function wpst_updated_category_image($term_id, $tt_id)
  114. {
  115. if (isset($_POST['category-image-id']) && '' !== $_POST['category-image-id'])
  116. {
  117. $image = $_POST['category-image-id'];
  118. update_term_meta($term_id, 'category-image-id', $image);
  119. }
  120. else
  121. {
  122. update_term_meta($term_id, 'category-image-id', '');
  123. }
  124. }
  125. /*
  126. * Add script
  127. * @since 1.0.0
  128. */
  129. public function wpst_add_script()
  130. { ?>
  131. <script>
  132. jQuery(document).ready( function($) {
  133. function ct_media_upload(button_class) {
  134. var _custom_media = true,
  135. _orig_send_attachment = wp.media.editor.send.attachment;
  136. $('body').on('click', button_class, function(e) {
  137. var button_id = '#'+$(this).attr('id');
  138. var send_attachment_bkp = wp.media.editor.send.attachment;
  139. var button = $(button_id);
  140. _custom_media = true;
  141. wp.media.editor.send.attachment = function(props, attachment){
  142. if ( _custom_media ) {
  143. $('#category-image-id').val(attachment.id);
  144. $('#category-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
  145. $('#category-image-wrapper .custom_media_image').attr('src',attachment.sizes.thumbnail.url).css('display','block');
  146. } else {
  147. return _orig_send_attachment.apply( button_id, [props, attachment] );
  148. }
  149. }
  150. wp.media.editor.open(button);
  151. return false;
  152. });
  153. }
  154. ct_media_upload('.ct_tax_media_button.button');
  155. $('body').on('click','.ct_tax_media_remove',function(){
  156. $('#category-image-id').val('');
  157. $('#category-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
  158. });
  159. // Thanks: http://stackoverflow.com/questions/15281995/wordpress-create-category-ajax-response
  160. $(document).ajaxComplete(function(event, xhr, settings) {
  161. if( settings.data ){
  162. var queryStringArr = settings.data.split('&');
  163. if( $.inArray('action=add-tag', queryStringArr) !== -1 ){
  164. var xml = xhr.responseXML;
  165. $response = $(xml).find('term_id').text();
  166. if($response!=""){
  167. // Clear the thumb image
  168. $('#category-image-wrapper').html('');
  169. }
  170. }
  171. }
  172. });
  173. });
  174. </script>
  175. <?php
  176. }
  177. }
  178.  
  179. $CT_TAX_META = new CT_TAX_META();
  180. $CT_TAX_META->wpst_init();
  181.  
  182. }
Add Comment
Please, Sign In to add comment