From eba126609f1b2695524ff45f54025bb1571ba700 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 10 Mar 2020 22:46:12 +0000 Subject: [PATCH] Media: Introduce preflight filter to `wp_delete_attachment()`. Introduces the filter `pre_delete_attachment` to allow developers to prevent or modify the deletion of attachments. This improves consistency with `wp_delete_post()` and `wp_trash_post()`. Props joemcgill, peterwilsoncc. Fixes #49597. git-svn-id: https://develop.svn.wordpress.org/trunk@47448 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 1db655af68..38582f2fa4 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -5620,6 +5620,20 @@ function wp_delete_attachment( $post_id, $force_delete = false ) { return wp_trash_post( $post_id ); } + /** + * Filters whether an attachment deletion should take place. + * + * @since 5.5.0 + * + * @param bool|null $delete Whether to go forward with deletion. + * @param WP_Post $post Post object. + * @param bool $force_delete Whether to bypass the Trash. + */ + $check = apply_filters( 'pre_delete_attachment', null, $post, $force_delete ); + if ( null !== $check ) { + return $check; + } + delete_post_meta( $post_id, '_wp_trash_meta_status' ); delete_post_meta( $post_id, '_wp_trash_meta_time' );