mardi 8 mars 2016

conflict in taxonomy image fields

i use this code to create custom taxonomy with image.

when i duplicate code and change "writer" to any other as "car". it work but with error in image field.

so i can not now choose an image for both taxonomies.

add_action( 'init', 'create_writers_nonhierarchical_taxonomy', 0 );

function create_writers_nonhierarchical_taxonomy() {

// Labels part for the GUI

  $labels = array(
    'name' => _x( 'Writers', 'taxonomy general name' ),
    'singular_name' => _x( 'Writer', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Writers' ),
    'popular_items' => __( 'Popular Writers' ),
    'all_items' => __( 'All Writers' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Writer' ), 
    'update_item' => __( 'Update Writer' ),
    'add_new_item' => __( 'Add New Writer' ),
    'new_item_name' => __( 'New Writer Name' ),
    'separate_items_with_commas' => __( 'Separate writers with commas' ),
    'add_or_remove_items' => __( 'Add or remove writers' ),
    'choose_from_most_used' => __( 'Choose from the most used writers' ),
    'menu_name' => __( 'Writers' ),
  ); 

// Now register the non-hierarchical taxonomy like tag

  register_taxonomy('writers','post',array(
    'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'show_in_nav_menus' =>  true,
    'has_archive'       =>  true,
    'rewrite' => array( 'slug' => 'writer', 'with_front' => false),
  ));
function writer_flush_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
add_action('init', 'writer_flush_rewrite');
}

/**
 * Plugin class
 **/
function load_wp_media_files() {
  wp_enqueue_media();
}
add_action( 'admin_enqueue_scripts', 'load_wp_media_files' );

if ( ! class_exists( 'CT_TAX_META' ) ) {

class CT_TAX_META {

  public function __construct() {
    //
  }

 /*
  * Initialize the class and start calling our hooks and filters
  * @since 1.0.0
 */
 public function init() {
   add_action( 'writers_add_form_fields', array ( $this, 'add_writers_image' ), 10, 2 );
   add_action( 'created_writers', array ( $this, 'save_writers_image' ), 10, 2 );
   add_action( 'writers_edit_form_fields', array ( $this, 'update_writers_image' ), 10, 2 );
   add_action( 'edited_writers', array ( $this, 'updated_writers_image' ), 10, 2 );
   add_action( 'admin_footer', array ( $this, 'add_script' ) );
 }

 /*
  * Add a form field in the new writers page
  * @since 1.0.0
 */
 public function add_writers_image ( $taxonomy ) { ?>
   <div class="form-field term-group">
     <label for="writers-image-id"><?php _e('Image', 'hero-theme'); ?></label>
     <input type="hidden" id="writers-image-id" name="writers-image-id" class="custom_media_url" value="">
     <div id="writers-image-wrapper"></div>
     <p>
       <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' ); ?>" />
       <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' ); ?>" />
    </p>
   </div>
 <?php
 }

 /*
  * Save the form field
  * @since 1.0.0
 */
 public function save_writers_image ( $term_id, $tt_id ) {
   if( isset( $_POST['writers-image-id'] ) && '' !== $_POST['writers-image-id'] ){
     $image = $_POST['writers-image-id'];
     add_term_meta( $term_id, 'writers-image-id', $image, true );
   }
 }

 /*
  * Edit the form field
  * @since 1.0.0
 */
 public function update_writers_image ( $term, $taxonomy ) { ?>
   <tr class="form-field term-group-wrap">
     <th scope="row">
       <label for="writers-image-id"><?php _e( 'Image', 'hero-theme' ); ?></label>
     </th>
     <td>
       <?php $image_id = get_term_meta ( $term -> term_id, 'writers-image-id', true ); ?>
       <input type="hidden" id="writers-image-id" name="writers-image-id" value="<?php echo $image_id; ?>">
       <div id="writers-image-wrapper">
         <?php if ( $image_id ) { ?>
           <?php echo wp_get_attachment_image ( $image_id, 'slider-small' ); ?>
         <?php } ?>
       </div>
       <p>
         <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' ); ?>" />
         <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' ); ?>" />
       </p>
     </td>
   </tr>
 <?php
 }

/*
 * Update the form field value
 * @since 1.0.0
 */
 public function updated_writers_image ( $term_id, $tt_id ) {
   if( isset( $_POST['writers-image-id'] ) && '' !== $_POST['writers-image-id'] ){
     $image = $_POST['writers-image-id'];
     update_term_meta ( $term_id, 'writers-image-id', $image );
   } else {
     update_term_meta ( $term_id, 'writers-image-id', '' );
   }
 }

/*
 * Add script
 * @since 1.0.0
 */
 public function add_script() { ?>
   <script>
     jQuery(document).ready( function($) {
       function ct_media_upload(button_class) {
         var _custom_media = true,
         _orig_send_attachment = wp.media.editor.send.attachment;
         $('body').on('click', button_class, function(e) {
           var button_id = '#'+$(this).attr('id');
           var send_attachment_bkp = wp.media.editor.send.attachment;
           var button = $(button_id);
           _custom_media = true;
           wp.media.editor.send.attachment = function(props, attachment){
             if ( _custom_media ) {
               $('#writers-image-id').val(attachment.id);
               $('#writers-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
               $('#writers-image-wrapper .custom_media_image').attr('src',attachment.sizes.thumbnail.url).css('display','block');
                var src = attachment.url;

               if (attachment.sizes.thumbnail) {
                 src = attachment.sizes.thumbnail.url;
               }
               $('#writers-image-wrapper .custom_media_image').attr('src',src).css('display','block');
             } else {
               return _orig_send_attachment.apply( button_id, [props, attachment] );
             }
            }
         wp.media.editor.open(button);
         return false;
       });
     }
     ct_media_upload('.ct_tax_media_button.button'); 
     $('body').on('click','.ct_tax_media_remove',function(){
       $('#writers-image-id').val('');
       $('#writers-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
     });
     // Thanks: http://ift.tt/1p4urps
     $(document).ajaxComplete(function(event, xhr, settings) {
       var queryStringArr = settings.data.split('&');
       if( $.inArray('action=add-tag', queryStringArr) !== -1 ){
         var xml = xhr.responseXML;
         $response = $(xml).find('term_id').text();
         if($response!=""){
           // Clear the thumb image
           $('#writers-image-wrapper').html('');
         }
       }
     });
   });
 </script>
 <?php }

  }

$CT_TAX_META = new CT_TAX_META();
$CT_TAX_META -> init();

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire