From 5b2eaae4cfe09613256c6249db7dbe9b5854198f Mon Sep 17 00:00:00 2001 From: Jake Spurlock Date: Thu, 4 Jun 2020 14:58:58 +0000 Subject: [PATCH] Media: Add an action for `wp_media_attach_action`. This action fires when media is attached/detached from a post. Fixes: #49614. Props: jonoaldersonwp, antpb, rebasaurus, donmhico. git-svn-id: https://develop.svn.wordpress.org/trunk@47909 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/media.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/wp-admin/includes/media.php b/src/wp-admin/includes/media.php index feebbd9bff..35080275ed 100644 --- a/src/wp-admin/includes/media.php +++ b/src/wp-admin/includes/media.php @@ -3724,13 +3724,24 @@ function wp_media_attach_action( $parent_id, $action = 'attach' ) { } else { $result = $wpdb->query( "UPDATE $wpdb->posts SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( $ids_string )" ); } - - foreach ( $ids as $att_id ) { - clean_attachment_cache( $att_id ); - } } if ( isset( $result ) ) { + foreach ( $ids as $att_id ) { + /** + * Fires when media is attached/detached from a post. + * + * @since 5.5 + * + * @param string $action Attach/detach action. + * @param int $att_id The attachment ID. + * @param int $parent_id Attachment parent ID. + */ + do_action( 'wp_media_attach_action', $action, $att_id, $parent_id ); + + clean_attachment_cache( $att_id ); + } + $location = 'upload.php'; $referer = wp_get_referer();