You just have to call destroy
method on the attachment model. This will both remove the attachment from the Media Library view, and send an ajax call to the backend to delete the attachment
in the database and all linked files in uploads directory.
You do not need to convert the attachment to JSON to get the id : you can directly manipulate the Backbone models. The selection
is a collection of several attachments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | ( function( $ ) { var _AttachmentDisplay = wp.media.view.Settings.AttachmentDisplay; wp.media.view.Settings.AttachmentDisplay = _AttachmentDisplay.extend({ render: function() { _AttachmentDisplay.prototype.render.apply(this, arguments); $('button.dmc').on('click', $.proxy(function(e){ e.preventDefault(); selection = this.controller.state().get('selection'); firstAttachment = selection.first(); var id = $(e.currentTarget).data("id"); if(currentselection.id == id) { selection.remove(firstAttachment); firstAttachment.destroy(); console.dir(wp.media.view.Attachment); newattachment = wp.media.attachment($(e.currentTarget).data("original")); selection.add(newattachment); } }, this)); } }); } )( jQuery ); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.