Definition:
function wp_delete_attachment( $post_id, $force_delete = false ) {}
Trashes or deletes an attachment.
When an attachment is permanently deleted, the file will also be removed. Deletion removes all post meta fields, taxonomy, comments, etc. associated with the attachment (except the main post).
Parameters
- int $post_id: Attachment ID.
- bool $force_delete: Whether to bypass trash and force deletion. Defaults to false.
Return values
returns:False on failure. Post data on success.
Defined filters
- wp_delete_file
apply_filters('wp_delete_file', $thumbfile)
- wp_delete_file
apply_filters('wp_delete_file', $intermediate['path'])
- wp_delete_file
apply_filters('wp_delete_file', $del_file)
- wp_delete_file
apply_filters('wp_delete_file', $file)
Defined actions
- delete_attachment
do_action('delete_attachment', $post_id);
- delete_comment
do_action( 'delete_comment', $comment_ids );
- deleted_comment
do_action( 'deleted_comment', $comment_ids );
- delete_postmeta
do_action( 'delete_postmeta', $post_meta_ids );
- deleted_postmeta
do_action( 'deleted_postmeta', $post_meta_ids );
- delete_post
do_action( 'delete_post', $post_id );
- deleted_post
do_action( 'deleted_post', $post_id );
Source code
function wp_delete_attachment( $post_id, $force_delete = false ) { global $wpdb; if ( !$post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) ) ) return $post; if ( 'attachment' != $post->post_type ) return false; if ( !$force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' != $post->post_status ) return wp_trash_post( $post_id ); delete_post_meta($post_id, '_wp_trash_meta_status'); delete_post_meta($post_id, '_wp_trash_meta_time'); $meta = wp_get_attachment_metadata( $post_id ); $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); $file = get_attached_file( $post_id ); if ( is_multisite() ) delete_transient( 'dirsize_cache' ); do_action('delete_attachment', $post_id); wp_delete_object_term_relationships($post_id, array('category', 'post_tag')); wp_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type)); $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND meta_value = %d", $post_id )); $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id )); if ( ! empty( $comment_ids ) ) { do_action( 'delete_comment', $comment_ids ); foreach ( $comment_ids as $comment_id ) wp_delete_comment( $comment_id, true ); do_action( 'deleted_comment', $comment_ids ); } $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id )); if ( !empty($post_meta_ids) ) { do_action( 'delete_postmeta', $post_meta_ids ); $in_post_meta_ids = "'" . implode("', '", $post_meta_ids) . "'"; $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id IN($in_post_meta_ids)" ); do_action( 'deleted_postmeta', $post_meta_ids ); } do_action( 'delete_post', $post_id ); $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $post_id )); do_action( 'deleted_post', $post_id ); $uploadpath = wp_upload_dir(); if ( ! empty($meta['thumb']) ) { // Don't delete the thumb if another attachment uses it if (! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $meta['thumb'] . '%', $post_id)) ) { $thumbfile = str_replace(basename($file), $meta['thumb'], $file); $thumbfile = apply_filters('wp_delete_file', $thumbfile); @ unlink( path_join($uploadpath['basedir'], $thumbfile) ); } } // remove intermediate and backup images if there are any foreach ( get_intermediate_image_sizes() as $size ) { if ( $intermediate = image_get_intermediate_size($post_id, $size) ) { $intermediate_file = apply_filters('wp_delete_file', $intermediate['path']); @ unlink( path_join($uploadpath['basedir'], $intermediate_file) ); } } if ( is_array($backup_sizes) ) { foreach ( $backup_sizes as $size ) { $del_file = path_join( dirname($meta['file']), $size['file'] ); $del_file = apply_filters('wp_delete_file', $del_file); @ unlink( path_join($uploadpath['basedir'], $del_file) ); } } $file = apply_filters('wp_delete_file', $file); if ( ! empty($file) ) @ unlink($file); clean_post_cache($post_id); return $post; }
3583
No comments yet... Be the first to leave a reply!