wp_media_attach_action( int $parent_id, string $action = 'attach' )
- Since
- 4.2.0
- Source
wp-admin/includes/media.php:3820
Compatibility
- WordPress
- since 4.2.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$parent_idint- Attachment parent ID.
$actionstringoptional- Attach/detach action. Accepts 'attach' or 'detach'.
Default 'attach'.Default:'attach'
Performance profile
How much work a call to wp_media_attach_action() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 5–76
- Plugin surface
- 1 hook
- Called by
- 0
Reaches the database via ->query().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 108.
Third-party callbacks on 'wp_media_attach_action' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()called directly - sqldatabase query
->query()called directly - cacheobject cache
wp_cache_delete()one call below wp_media_attach_action()
Further down the call graph this can also reach query, option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 19 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_media_attach_action() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5 | none |
current_user_can() && empty($ids) && !isset($result) | 20–21 | current_user_can() |
!current_user_can() && empty($ids) && !isset($result) | 26–27 | current_user_can(), __(), wp_die() |
current_user_can() && !empty($ids) && $action !== "attach" && !isset($result) | 33–34 | current_user_can(), ->query() |
current_user_can() && !empty($ids) && $action === "attach" && !isset($result) | 38–39 | current_user_can(), ->prepare(), ->query() |
!current_user_can() && !empty($ids) && $action !== "attach" && !isset($result) | 39–40 | current_user_can(), __(), wp_die(), ->query() |
current_user_can() && empty($ids) && isset($result) | 42–47 | current_user_can(), wp_get_referer(), wp_redirect(), exit() |
!current_user_can() && !empty($ids) && $action === "attach" && !isset($result) | 44–45 | current_user_can(), __(), wp_die(), ->prepare(), ->query() |
!current_user_can() && empty($ids) && isset($result) | 48–53 | current_user_can(), __(), wp_die(), wp_get_referer(), wp_redirect(), exit() |
current_user_can() && empty($ids) && isset($result) && $referer | 49–52 | current_user_can(), wp_get_referer(), remove_query_arg(), wp_redirect(), exit() |
current_user_can() && !empty($ids) && $action !== "attach" && isset($result) | 55–60 | current_user_can(), ->query(), wp_get_referer(), wp_redirect(), exit() |
!current_user_can() && empty($ids) && isset($result) && $referer | 55–58 | current_user_can(), __(), wp_die(), wp_get_referer(), remove_query_arg(), wp_redirect(), exit() |
7 further outcomes, up to 76 instructions
current_user_can() && !empty($ids) && $action === "attach" && isset($result) | 60–65 | current_user_can(), ->prepare(), ->query(), wp_get_referer(), wp_redirect(), exit() |
!current_user_can() && !empty($ids) && $action !== "attach" && isset($result) | 61–66 | current_user_can(), __(), wp_die(), ->query(), wp_get_referer(), wp_redirect(), exit() |
current_user_can() && !empty($ids) && $action !== "attach" && isset($result) && $referer | 62–65 | current_user_can(), ->query(), wp_get_referer(), remove_query_arg(), wp_redirect(), exit() |
!current_user_can() && !empty($ids) && $action === "attach" && isset($result) | 66–71 | current_user_can(), __(), wp_die(), ->prepare(), ->query(), wp_get_referer(), wp_redirect(), exit() |
current_user_can() && !empty($ids) && $action === "attach" && isset($result) && $referer | 67–70 | current_user_can(), ->prepare(), ->query(), wp_get_referer(), remove_query_arg(), wp_redirect(), exit() |
!current_user_can() && !empty($ids) && $action !== "attach" && isset($result) && $referer | 68–71 | current_user_can(), __(), wp_die(), ->query(), wp_get_referer(), remove_query_arg(), wp_redirect(), exit() |
!current_user_can() && !empty($ids) && $action === "attach" && isset($result) && $referer | 73–76 | current_user_can(), __(), wp_die(), ->prepare(), ->query(), wp_get_referer(), remove_query_arg(), wp_redirect(), exit() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 108 | 5–76 | 13 | |
| 8.5 | 108 | 5–76 | 13 | |
| 8.4 | 108 | 5–76 | 13 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 114 | 5–81 | 13 | |
| 8.2 | 114 | 5–81 | 13 | |
| 8.1 | 114 | 5–81 | 13 | |
| 7.4 | 114 | 5–81 | 13 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while wp_media_attach_action() runs, in this order:
- do_action( wp_media_attach_action )actionline 3864 (+44 into the body)
Fires when media is attached or detached from a post.
Uses · 10
- current_user_can()Returns whether the current user has the specified capability.
- wp_die()Kills WordPress execution and displays HTML page with an error message.
- __()Retrieves the translation of $text.
- do_action()Calls the callback functions that have been added to an action hook.
- clean_attachment_cache()Will clean the attachment in the cache.
- wp_get_referer()Retrieves referer from '_wp_http_referer' or HTTP referer.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- remove_query_arg()Removes an item or items from a query string.
- add_query_arg()Retrieves a modified URL query string.
- wp_redirect()Redirects to another page.
Source code
function wp_media_attach_action( $parent_id, $action = 'attach' ) { global $wpdb; if ( ! $parent_id ) { return; } if ( ! current_user_can( 'edit_post', $parent_id ) ) { wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); } $ids = array(); foreach ( (array) $_REQUEST['media'] as $attachment_id ) { $attachment_id = (int) $attachment_id; if ( ! current_user_can( 'edit_post', $attachment_id ) ) { continue; } $ids[] = $attachment_id; } if ( ! empty( $ids ) ) { $ids_string = implode( ',', $ids ); if ( 'attach' === $action ) { $result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $ids_string )", $parent_id ) ); } else { $result = $wpdb->query( "UPDATE $wpdb->posts SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( $ids_string )" ); } } if ( isset( $result ) ) { foreach ( $ids as $attachment_id ) { /** * Fires when media is attached or detached from a post. * * @since 5.5.0 * * @param string $action Attach/detach action. Accepts 'attach' or 'detach'. * @param int $attachment_id The attachment ID. * @param int $parent_id Attachment parent ID. */ do_action( 'wp_media_attach_action', $action, $attachment_id, $parent_id ); clean_attachment_cache( $attachment_id ); } $location = 'upload.php'; $referer = wp_get_referer(); if ( $referer ) { if ( str_contains( $referer, 'upload.php' ) ) { $location = remove_query_arg( array( 'attached', 'detach' ), $referer ); } } $key = 'attach' === $action ? 'attached' : 'detach'; $location = add_query_arg( array( $key => $result ), $location ); wp_redirect( $location ); exit; }}Changelog
Introduced in 4.2.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-admin/includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.