返回介绍

wp_delete_attachment()

发布于 2017-09-11 11:49:00 字数 7300 浏览 1067 评论 0 收藏 0

wp_delete_attachment( int $post_id,  bool $force_delete = false )

Trash or delete an attachment.


description

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).

The attachment is moved to the trash instead of permanently deleted unless trash for media is disabled, item is already in the trash, or $force_delete is true.


参数

$post_id

(int) (Required) Attachment ID.

$force_delete

(bool) (Optional) Whether to bypass trash and force deletion.

Default value: false


返回值

(mixed) False on failure. Post data on success.


源代码

File: wp-includes/post.php

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' );

	/**
	 * Fires before an attachment is deleted, at the start of wp_delete_attachment().
	 *
	 * @since 2.0.0
	 *
	 * @param int $post_id Attachment ID.
	 */
	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));

	// Delete all for any posts.
	delete_metadata( 'post', null, '_thumbnail_id', $post_id, true );

	wp_defer_comment_counting( true );

	$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ));
	foreach ( $comment_ids as $comment_id ) {
		wp_delete_comment( $comment_id, true );
	}

	wp_defer_comment_counting( false );

	$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ));
	foreach ( $post_meta_ids as $mid )
		delete_metadata_by_mid( 'post', $mid );

	/** This action is documented in wp-includes/post.php */
	do_action( 'delete_post', $post_id );
	$result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) );
	if ( ! $result ) {
		return false;
	}
	/** This action is documented in wp-includes/post.php */
	do_action( 'deleted_post', $post_id );

	$uploadpath = wp_get_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", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id)) ) {
			$thumbfile = str_replace(basename($file), $meta['thumb'], $file);
			/** This filter is documented in wp-includes/functions.php */
			$thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
			@ unlink( path_join($uploadpath['basedir'], $thumbfile) );
		}
	}

	// Remove intermediate and backup images if there are any.
	if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
		foreach ( $meta['sizes'] as $size => $sizeinfo ) {
			$intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
			/** This filter is documented in wp-includes/functions.php */
			$intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
			@ 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'] );
			/** This filter is documented in wp-includes/functions.php */
			$del_file = apply_filters( 'wp_delete_file', $del_file );
			@ unlink( path_join($uploadpath['basedir'], $del_file) );
		}
	}

	wp_delete_file( $file );

	clean_post_cache( $post );

	return $post;
}

更新日志

Versiondescription
2.0.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: wp_get_upload_dir()
  • wp-includes/functions.php: wp_delete_file
  • wp-includes/functions.php: wp_delete_file()
  • wp-includes/wp-db.php: wpdb::esc_like()
  • wp-includes/load.php: is_multisite()
  • wp-includes/functions.php: path_join()
  • wp-includes/taxonomy.php: wp_delete_object_term_relationships()
  • wp-includes/taxonomy.php: get_object_taxonomies()
  • wp-includes/plugin.php: do_action()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: delete_transient()
  • wp-includes/post.php: clean_post_cache()
  • wp-includes/post.php: wp_get_attachment_metadata()
  • wp-includes/post.php: delete_attachment
  • wp-includes/post.php: wp_trash_post()
  • wp-includes/post.php: delete_post
  • wp-includes/post.php: deleted_post
  • wp-includes/post.php: delete_post_meta()
  • wp-includes/post.php: get_post_meta()
  • wp-includes/post.php: get_attached_file()
  • wp-includes/wp-db.php: wpdb::get_row()
  • wp-includes/wp-db.php: wpdb::get_col()
  • wp-includes/wp-db.php: wpdb::delete()
  • wp-includes/wp-db.php: wpdb::prepare()
  • wp-includes/comment.php: wp_defer_comment_counting()
  • wp-includes/comment.php: wp_delete_comment()
  • wp-includes/meta.php: delete_metadata()
  • wp-includes/meta.php: delete_metadata_by_mid()
  • Show 23 more uses Hide more uses

Used By

  • wp-admin/includes/class-file-upload-upgrader.php: File_Upload_Upgrader::cleanup()
  • wp-admin/includes/import.php: wp_import_cleanup()
  • wp-includes/post.php: wp_delete_post()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    To delete an attachment with an ID of ’76’:

    
    <?php wp_delete_attachment( 76 ); ?>
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文