• Resolved merckdelmoro

    (@merckdelmoro)


    there is a bug when selecting attachments, when there a file doesn’t have a thumbnail it will trigger an error I added a code to the plugin on my site to fix it, you can change the script by this one

    function kgm_add_attachment() {
    	var mediaframes = wp.media.frames.items = wp.media({
    		'multiple': true
    	});
    	mediaframes.on('select', function() {
    		var attachment = mediaframes.state().get('selection').toJSON();			
    		for(var i = 0; i < mediaframes.state().get('selection').length; i++)
    		{
    			var currentIDS = jQuery('#attachment_ids').val();
    
    			if (currentIDS == '') {
    				currentIDS = currentIDS+attachment[i].id				   
    			} else {
    				currentIDS = currentIDS+','+attachment[i].id
    			}
    			if ( (typeof attachment[i].sizes !== undefined ) && ( typeof attachment[i].sizes !== 'undefined' ) ) {
    				if ( attachment[i].sizes.thumbnail !== undefined  ) url_image=attachment[i].sizes.thumbnail.url; 
    				else if (attachment[i].sizes.medium !== undefined ) url_image=attachment[i].sizes.medium.url;
    				else url_image=attachment[i].sizes.full.url;
    			}
    			var file_image = (typeof attachment[i].sizes !== undefined ) && ( typeof attachment[i].sizes !== 'undefined' ) ? '<img src="'+url_image+'" style="max-width:150px;" /></br>' : '';
    			jQuery('#attachment_ids').val(currentIDS);
    			jQuery('.details').append('<li data-id="'+attachment[i].id+'">'+file_image+'+attachment[i].title+' <b>['+attachment[i].mime+']</b><div class="remove dashicons dashicons-dismiss" onClick="kgm_remove_attachment(this)"></div></li>');
    		}				
    	});
    	mediaframes.open();
    }
    
    function kgm_remove_attachment(id) {
    	var old = jQuery(id).parent();
    	old.remove();
    
    	var currentIDS = '';
    	jQuery('.details li').each(function(){
    		if (currentIDS == '') {
    			currentIDS = currentIDS+jQuery(this).data('id');
    		} else {
    			currentIDS = currentIDS+','+jQuery(this).data('id')
    		}
    	});
    	jQuery('#attachment_ids').val(currentIDS);
    }

    I hope this helps

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author kgmservizi

    (@kgmservizi)

    Thanks @merckdelmoro ! Try latest version, I’ve fixed.

    Regards

    Thread Starter merckdelmoro

    (@merckdelmoro)

    noted thanks

    Thread Starter merckdelmoro

    (@merckdelmoro)

    @kgmservizi I’ve test the new update version I think instead of using url_image=attachment[i].url for no thumbnail files I think you can use the default file image of wordpress here is the code

    
    if (attachment[i].sizes) {
    				if (attachment[i].sizes.thumbnail !== undefined  ) url_image=attachment[i].sizes.thumbnail.url; 
    	    		else if (attachment[i].sizes.medium !== undefined ) url_image=attachment[i].sizes.medium.url;
    	    		else if (attachment[i].sizes.full !== undefined ) url_image=attachment[i].sizes.full.url;
    	    	} else {
    
    	    		url_image = attachment[i].icon;
    	    	}
    • This reply was modified 2 years, 11 months ago by merckdelmoro.
    • This reply was modified 2 years, 11 months ago by merckdelmoro.
    • This reply was modified 2 years, 11 months ago by merckdelmoro.
    • This reply was modified 2 years, 11 months ago by merckdelmoro.
    Plugin Author kgmservizi

    (@kgmservizi)

    Thanks @merckdelmoro I’ve used your approach in latest release. You can check?

    Regards

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘There is a Bug when selecting attachments’ is closed to new replies.