Advertisement
Guest User

Images for taxonomy POST_TAG

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