Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

logged in as admin and trying to code that deletes posts to delete an attachment.

got,

<?php if (current_user_can('edit_post', $attachment->ID)) 
echo "<a href='" . wp_nonce_url("/wp-admin/post.php?action=delete&amp;post=".$attachment->ID."", 'delete-post_' . $attachment->ID) . "'>Delete</a>" ?>

but it just takes me to a page that says;

Are you sure you want to do this? Please try again.

and the please try again links back to the post...

is it something to do with the delete-post_

best, dan.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

ended up using ajax in the end...

html;

<a class="remImage" name="<?php echo $attachment->ID; ?>" href="#"><?php _e('delete');?></a>  <input type="hidden" id="att_remove" name="att_remove[]" value="<?php echo $attachment->ID; ?>" />
<input type="hidden" name="nonce" id="nonce" value="<?php echo wp_create_nonce( 'delete_attachment' ); ?>" />

jquery

$('.remImage').live('click', function() {
    var attID = jQuery(this).attr('name');
    jQuery.ajax({
        type: 'post',
        url: '/wp-admin/admin-ajax.php',
        data: {
            action: 'delete_attachment',
            att_ID: jQuery(this).attr('name'),
            _ajax_nonce: jQuery('#nonce').val(),
            post_type: 'attachment'
        },
        success: function() {
            console.log('#file-'+attID)
            $('#file-'+attID).fadeOut();    
        }
});

in functions.php

add_action( 'wp_ajax_delete_attachment', 'delete_attachment' );
function delete_attachment( $post ) {
    //echo $_POST['att_ID'];
    $msg = 'Attachment ID [' . $_POST['att_ID'] . '] has been deleted!';
    if( wp_delete_attachment( $_POST['att_ID'], true )) {
        echo $msg;
    }
    die();
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.