[wp-trac] [WordPress Trac] #49614: Add an action for wp_media_attach_action events

WordPress Trac noreply at wordpress.org
Tue Mar 10 13:21:08 UTC 2020


#49614: Add an action for wp_media_attach_action events
----------------------------+-----------------------------
 Reporter:  jonoaldersonwp  |      Owner:  (none)
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  General         |    Version:
 Severity:  normal          |   Keywords:
  Focuses:  administration  |
----------------------------+-----------------------------
 When a user attaches or detaches media to/from a post,
 `wp_media_attach_action()` is called.

 See https://github.com/WordPress/WordPress/blob/master/wp-
 admin/includes/media.php#L3694

 This function sets or removes a `post_parent` value on an attachment post
 by updating `$wpdb->posts`.

 At the moment, there's no way for plugins or integrations to take action
 based on these events; making it incredibly difficult to alter or add to
 the behaviour or outcomes of these processes.

 Specifically, it limits the ability to maintain an up-to-date overview of
 the relationships between posts and their attachments.

 To address this, **we should add an action hook following the successful
 execution of the query** (i.e., if `$results` returns true), using either
 of the following approaches:
   - `do_action('wp_media_attach_action', $attach, $attachment_id,
 $parent_id)` (where `$attach` is `true` for 'attach' actions, and `false`
 for 'detach' actions); OR
   - Conditionally either `do_action('wp_media_attach_action_attach',
 $attachment_id, $parent_id)` or
 `do_action('wp_media_attach_action_detach', $attachment_id, $parent_id)`.

 This will allow plugins and integrations to take arbitrary action based on
 attaching/detaching media, with the option to consider the type of action,
 the parent, and the attachment itself.

 Note that I considered that `clean_attachment_cache()` is hookable (and
 executed in the `wp_media_attach_action()` function), but only passes the
 `post_id`; lacking the critical context of the parent, and the
 attachment/detachment status. This also appears to fire before any
 validation that the query was successful, which strikes me as strange.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/49614>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list