Support

Account

Home Forums Add-ons Gallery Field JavaScript – Gallery Item Remove Click Reply To: JavaScript – Gallery Item Remove Click

  • If there’s a better way to do this, let me know. This is how I was able to do it:

    ( function( $ ) {
    
    	if( 'object' !== typeof acf ) {
    		return;
    	}
    	
    	/**
    	 * Override Gallery Field onClickRemove method.
    	 * This method removes the attachment from the galllery but not the server.
    	 * Runs on media [x] click and "Remove" in gallery sidebar.
    	 *
    	 * @param e Event
    	 * @param $el DOMObject
    	 *
    	 * @return void
    	 */
    	acf.models.GalleryField.prototype.onClickRemove = function( e, $el ) {
    		
    		e.preventDefault();
    		e.stopPropagation();
    
    		let attachment_id = $el.data( 'id' );
    
    		if( attachment_id ) {
    			attachment_id.removeAttachment( attachment_id );
    		}
    		
    		/* AJAX to delete the physical attachment. */
    		
    	};
    
    } )( jQuery );