wppaste
WordPress

wp_media_attach_action( int $parent_id, string $action = 'attach' )

Since
4.2.0
Source
wp-admin/includes/media.php:3839
Encapsulates the logic for Attach/Detach actions.

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

Reaches the database via ->query().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
5–76

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 108.

Plugin surface
1 hook

Third-party callbacks on 'wp_media_attach_action' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_action()called directly
  • sqldatabase query->query()called directly
  • cacheobject cachewp_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.

WhenInstructionsCalls it makes
always5none
current_user_can() && empty($ids) && !isset($result)20–21current_user_can()
!current_user_can() && empty($ids) && !isset($result)26–27current_user_can(), __(), wp_die()
current_user_can() && !empty($ids) && $action !== "attach" && !isset($result)33–34current_user_can(), ->query()
current_user_can() && !empty($ids) && $action === "attach" && !isset($result)38–39current_user_can(), ->prepare(), ->query()
!current_user_can() && !empty($ids) && $action !== "attach" && !isset($result)39–40current_user_can(), __(), wp_die(), ->query()
current_user_can() && empty($ids) && isset($result)42–47current_user_can(), wp_get_referer(), wp_redirect(), exit()
!current_user_can() && !empty($ids) && $action === "attach" && !isset($result)44–45current_user_can(), __(), wp_die(), ->prepare(), ->query()
!current_user_can() && empty($ids) && isset($result)48–53current_user_can(), __(), wp_die(), wp_get_referer(), wp_redirect(), exit()
current_user_can() && empty($ids) && isset($result) && $referer49–52current_user_can(), wp_get_referer(), remove_query_arg(), wp_redirect(), exit()
current_user_can() && !empty($ids) && $action !== "attach" && isset($result)55–60current_user_can(), ->query(), wp_get_referer(), wp_redirect(), exit()
!current_user_can() && empty($ids) && isset($result) && $referer55–58current_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–65current_user_can(), ->prepare(), ->query(), wp_get_referer(), wp_redirect(), exit()
!current_user_can() && !empty($ids) && $action !== "attach" && isset($result)61–66current_user_can(), __(), wp_die(), ->query(), wp_get_referer(), wp_redirect(), exit()
current_user_can() && !empty($ids) && $action !== "attach" && isset($result) && $referer62–65current_user_can(), ->query(), wp_get_referer(), remove_query_arg(), wp_redirect(), exit()
!current_user_can() && !empty($ids) && $action === "attach" && isset($result)66–71current_user_can(), __(), wp_die(), ->prepare(), ->query(), wp_get_referer(), wp_redirect(), exit()
current_user_can() && !empty($ids) && $action === "attach" && isset($result) && $referer67–70current_user_can(), ->prepare(), ->query(), wp_get_referer(), remove_query_arg(), wp_redirect(), exit()
!current_user_can() && !empty($ids) && $action !== "attach" && isset($result) && $referer68–71current_user_can(), __(), wp_die(), ->query(), wp_get_referer(), remove_query_arg(), wp_redirect(), exit()
!current_user_can() && !empty($ids) && $action === "attach" && isset($result) && $referer73–76current_user_can(), __(), wp_die(), ->prepare(), ->query(), wp_get_referer(), remove_query_arg(), wp_redirect(), exit()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1085–7613
8.51085–7613
8.41085–76136 fewer instructions than PHP 8.3
8.31145–8113
8.21145–8113
8.11145–8113
7.41145–8113

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:

  1. do_action( wp_media_attach_action )actionline 3883 (+44 into the body)

    Fires when media is attached or detached from a post.

Uses · 10

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.