I think I’m on the right track with what I need to do, at least for doing one of the ways it could be done. I’m not really sure though, I could be way off for all I know.
This is part of the code I needed help with last night for resizing the uploaded image files, but now I am trying to figure out how to use the wp_delete_attachment($id) function to delete images attached to the posts.
So, I have this jQuery script that adds/ removes extra fields, for uploading more images to attach. For every added image there is a link to remove them. Clicking remove has only ever just remove the div which the link clicked was contained by, and when saving the post the attached file would show up as still attached.
In my click event handler for the remove link, I need to first run function that will call the wp_delete_attachment(), to permanently delete the file/attachment, THEN I can use jQuery to remove the div of that attachment from the page. That way when saving the post it will have been deleted and not show up again still.
Here’s my functions I’ve been trying to work with:
// a function that returns the delete attachment php function,
// and a message saying Deleted that fades in and out.
jQuery(function() {
function delete_att( attID ) {
var div = jQuery('#img_uploads'),
msg = $('div').html('<strong>Attachment Deleted!</strong>').fadeIn().delay(200).fadeOut().appendTo('div');
return '<?php wp_delete_attachment( ' + attID + ', true ); ?>', msg;
}
And the click handler which calls this function and then removes the containing div element from the page is:
jQuery('.remImage').live('click', function() {
if( size > 1 ) {
var postID = jQuery('#attID').val()
delete_att( postID );
//jQuery(this).parents('.attchmt').find('#attID');
jQuery(this).parents('.attchmt').detach();
size--;
}
return false;
});
});
The code to add extra fields is between these two parts but these are the key pieces for what I need help with. Currently, the way I have it coded here I am getting this error message. idk wtf this means exactly though lol.
Error: uncaught exception: [Exception... "Node cannot be inserted at the specified point in the hierarchy" code: "3" nsresult: "0x80530003 (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)" location: "http://code.jquery.com/jquery-1.4.4.min.js Line: 113"]
So here is the full code which I am using, exactly. https://gist.github.com/802465
Just trying to pass info to/from jQuery and Php and that’s always a pain in the ass, (for me).