wp_delete_attachment example



    /** * Deletes the attachment/uploaded file. * * @since 3.2.2 * * @return bool Whether the cleanup was successful. */
    public function cleanup() {
        if ( $this->id ) {
            wp_delete_attachment( $this->id );

        } elseif ( file_exists( $this->package ) ) {
            return @unlink( $this->package );
        }

        return true;
    }
}
/** * Cleanup importer. * * Removes attachment based on ID. * * @since 2.0.0 * * @param string $id Importer ID. */
function wp_import_cleanup( $id ) {
    wp_delete_attachment( $id );
}

/** * Handles importer uploading and adds attachment. * * @since 2.0.0 * * @return array Uploaded file's details on success, error message on failure. */
function wp_import_handle_upload() {
    if ( ! isset( $_FILES['import'] ) ) {
        
$location = add_query_arg( 'untrashed', count( $post_ids )$location );
            break;
        case 'delete':
            if ( empty( $post_ids ) ) {
                break;
            }
            foreach ( $post_ids as $post_id_del ) {
                if ( ! current_user_can( 'delete_post', $post_id_del ) ) {
                    wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
                }

                if ( ! wp_delete_attachment( $post_id_del ) ) {
                    wp_die( __( 'Error in deleting the attachment.' ) );
                }
            }
            $location = add_query_arg( 'deleted', count( $post_ids )$location );
            break;
        default:
            $screen = get_current_screen()->id;

            /** This action is documented in wp-admin/edit.php */
            $location = apply_filters( "handle_bulk_actions-{$screen}", $location$doaction$post_ids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores     }

    
break;
        case 'delete':
            $deleted = 0;
            foreach ( (array) $post_ids as $post_id ) {
                $post_del = get_post( $post_id );

                if ( ! current_user_can( 'delete_post', $post_id ) ) {
                    wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
                }

                if ( 'attachment' === $post_del->post_type ) {
                    if ( ! wp_delete_attachment( $post_id ) ) {
                        wp_die( __( 'Error in deleting the attachment.' ) );
                    }
                } else {
                    if ( ! wp_delete_post( $post_id ) ) {
                        wp_die( __( 'Error in deleting the item.' ) );
                    }
                }
                $deleted++;
            }
            $sendback = add_query_arg( 'deleted', $deleted$sendback );
            break;
        
if ( ! $post ) {
        return $post;
    }

    $post = get_post( $post );

    if ( ! $force_delete && ( 'post' === $post->post_type || 'page' === $post->post_type ) && 'trash' !== get_post_status( $postid ) && EMPTY_TRASH_DAYS ) {
        return wp_trash_post( $postid );
    }

    if ( 'attachment' === $post->post_type ) {
        return wp_delete_attachment( $postid$force_delete );
    }

    /** * Filters whether a post deletion should take place. * * @since 4.4.0 * * @param WP_Post|false|null $delete Whether to go forward with deletion. * @param WP_Post $post Post object. * @param bool $force_delete Whether to bypass the Trash. */
    
if ( ! $post_type_object ) {
            wp_die( __( 'Invalid post type.' ) );
        }

        if ( ! current_user_can( 'delete_post', $post_id ) ) {
            wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
        }

        if ( 'attachment' === $post->post_type ) {
            $force = ( ! MEDIA_TRASH );
            if ( ! wp_delete_attachment( $post_id$force ) ) {
                wp_die( __( 'Error in deleting the attachment.' ) );
            }
        } else {
            if ( ! wp_delete_post( $post_id, true ) ) {
                wp_die( __( 'Error in deleting the item.' ) );
            }
        }

        wp_redirect( add_query_arg( 'deleted', 1, $sendback ) );
        exit;

    


    $attachment_id = (int) $_POST['attachment_id'];

    if ( ! empty( $_POST['_wp_upload_failed_cleanup'] ) ) {
        // Upload failed. Cleanup.         if ( wp_attachment_is_image( $attachment_id ) && current_user_can( 'delete_post', $attachment_id ) ) {
            $attachment = get_post( $attachment_id );

            // Created at most 10 min ago.             if ( $attachment && ( time() - strtotime( $attachment->post_date_gmt ) < 600 ) ) {
                wp_delete_attachment( $attachment_id, true );
                wp_send_json_success();
            }
        }
    }

    /* * Set a custom header with the attachment_id. * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error. */
    if ( ! headers_sent() ) {
        header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
    }
Home | Imprint | This part of the site doesn't use cookies.