Advertisement
rejuancse

post-category-image

Dec 24th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.22 KB | None | 0 0
  1. <?php
  2.  
  3. /*-------------------------------------------------------
  4. * ------- Category Image Upload : Home Style: 4 ---------
  5. --------------------------------------------------------- */
  6. if ( ! class_exists( 'Gutenwp_Post_Category_Image' ) ) {
  7.  
  8. class Gutenwp_Post_Category_Image {
  9.  
  10. public function __construct() { }
  11.  
  12. /*
  13. * Initialize the class and start calling our hooks and filters
  14. */
  15. public function init() {
  16.    add_action( 'category_add_form_fields', array ( $this, 'add_category_image' ), 10, 2 );
  17.    add_action( 'created_category', array ( $this, 'save_category_image' ), 10, 2 );
  18.    add_action( 'category_edit_form_fields', array ( $this, 'update_category_image' ), 10, 2 );
  19.    add_action( 'edited_category', array ( $this, 'updated_category_image' ), 10, 2 );
  20.    add_action( 'admin_enqueue_scripts', array( $this, 'load_media' ) );
  21.    add_action( 'admin_footer', array ( $this, 'add_script' ) );
  22. }
  23.  
  24. public function load_media() {
  25.     wp_enqueue_media();
  26. }
  27.  
  28. /*-------------------------------------------------
  29. * --- Add a form field in the new category page ---
  30. --------------------------------------------------- */
  31. public function add_category_image ( $taxonomy ) { ?>
  32.     <div class="form-field term-group">
  33.         <label for="category-image-id"><?php _e('Image', 'gutenwp'); ?></label>
  34.         <input type="hidden" id="category-image-id" name="category-image-id" class="custom_media_url" value="">
  35.         <div id="category-image-wrapper"></div>
  36.         <p>
  37.            <input type="button" class="button button-secondary gutenwp_tax_media_button" id="gutenwp_tax_media_button" name="gutenwp_tax_media_button" value="<?php _e( 'Add Image', 'gutenwp' ); ?>" />
  38.            <input type="button" class="button button-secondary gutenwp_tax_media_remove" id="gutenwp_tax_media_remove" name="gutenwp_tax_media_remove" value="<?php _e( 'Remove Image', 'gutenwp' ); ?>" />
  39.         </p>
  40.     </div>
  41. <?php }
  42.  
  43.  
  44. /*--------------------------------------
  45. * -------- Save the form field ---------
  46. ---------------------------------------- */
  47. public function save_category_image ( $term_id, $tt_id ) {
  48.     if( isset( $_POST['category-image-id'] ) && '' !== $_POST['category-image-id'] ){
  49.         $image = $_POST['category-image-id'];
  50.         add_term_meta( $term_id, 'category-image-id', $image, true );
  51.     }
  52. }
  53.  
  54. /*--------------------------------------
  55. * -------- Edit The Form Field ---------
  56. ---------------------------------------- */
  57. public function update_category_image ( $term, $taxonomy ) { ?>
  58.     <tr class="form-field term-group-wrap">
  59.         <th scope="row">
  60.             <label for="category-image-id"><?php _e( 'Image', 'gutenwp' ); ?></label>
  61.         </th>
  62.         <td>
  63.             <?php $image_id = get_term_meta ( $term -> term_id, 'category-image-id', true ); ?>
  64.             <input type="hidden" id="category-image-id" name="category-image-id" value="<?php echo $image_id; ?>">
  65.             <div id="category-image-wrapper">
  66.                 <?php if ( $image_id ) { ?>
  67.                 <?php echo wp_get_attachment_image ( $image_id, 'thumbnail' ); ?>
  68.                 <?php } ?>
  69.             </div>
  70.             <p>
  71.                 <input type="button" class="button button-secondary gutenwp_tax_media_button" id="gutenwp_tax_media_button" name="gutenwp_tax_media_button" value="<?php _e( 'Add Image', 'gutenwp' ); ?>" />
  72.                 <input type="button" class="button button-secondary gutenwp_tax_media_remove" id="gutenwp_tax_media_remove" name="gutenwp_tax_media_remove" value="<?php _e( 'Remove Image', 'gutenwp' ); ?>" />
  73.             </p>
  74.         </td>
  75.     </tr>
  76. <?php }
  77.  
  78.  
  79. /*--------------------------------------
  80. * ---- Update the form field value -----
  81. ---------------------------------------- */
  82. public function updated_category_image ( $term_id, $tt_id ) {
  83.     if( isset( $_POST['category-image-id'] ) && '' !== $_POST['category-image-id'] ){
  84.         $image = $_POST['category-image-id'];
  85.         update_term_meta ( $term_id, 'category-image-id', $image );
  86.     } else {
  87.         update_term_meta ( $term_id, 'category-image-id', '' );
  88.     }
  89. }
  90.  
  91. /*------------------------------------
  92. * ----------- Add script -------------
  93. -------------------------------------- */
  94. public function add_script() { ?>
  95.     <script>
  96.         jQuery(document).ready( function($) {
  97.             function gutenwp_media_upload(button_class) {
  98.                 var _custom_media = true,
  99.                 _orig_send_attachment = wp.media.editor.send.attachment;
  100.                 $('body').on('click', button_class, function(e) {
  101.                     var button_id = '#'+$(this).attr('id');
  102.                     var send_attachment_bkp = wp.media.editor.send.attachment;
  103.                     var button = $(button_id);
  104.                     _custom_media = true;
  105.                     wp.media.editor.send.attachment = function(props, attachment){
  106.                         if ( _custom_media ) {
  107.                            $('#category-image-id').val(attachment.id);
  108.                            $('#category-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
  109.                            $('#category-image-wrapper .custom_media_image').attr('src',attachment.url).css('display','block');
  110.                         } else {
  111.                            return _orig_send_attachment.apply( button_id, [props, attachment] );
  112.                         }
  113.                     }
  114.                     wp.media.editor.open(button);
  115.                     return false;
  116.                 });
  117.             }
  118.  
  119.             gutenwp_media_upload('.gutenwp_tax_media_button.button');
  120.             $('body').on('click','.gutenwp_tax_media_remove',function(){
  121.                 $('#category-image-id').val('');
  122.                 $('#category-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0; padding:0; max-height:100px; float:none;" />');
  123.             });
  124.  
  125.            
  126.             $(document).ajaxComplete(function(event, xhr, settings) {
  127.                 var queryStringArr = settings.data.split('&');
  128.                 if( $.inArray('action=add-tag', queryStringArr) !== -1 ){
  129.                     var xml = xhr.responseXML;
  130.                     $response = $(xml).find('term_id').text();
  131.                     if($response!=""){
  132.                         // Clear the thumb image
  133.                         $('#category-image-wrapper').html('');
  134.                     }
  135.                 }
  136.             });
  137.         });
  138.     </script>
  139.  
  140.     <?php } }
  141.     $Gutenwp_Post_Category_Image = new Gutenwp_Post_Category_Image();
  142.     $Gutenwp_Post_Category_Image -> init();
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement