Title: wp_copy_parent_attachment_properties
Published: April 3, 2024
Last modified: May 20, 2026

---

# wp_copy_parent_attachment_properties( string $cropped, int $parent_attachment_id, string $context = '' ): array

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/?output_format=md#wp--skip-link--target)

Copy parent attachment properties to newly cropped image.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/?output_format=md#parameters)󠁿

 `$cropped`stringrequired

Path to the cropped image file.

`$parent_attachment_id`intrequired

Parent file Attachment ID.

`$context`stringoptional

Control calling the function.

Default:`''`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/?output_format=md#return)󠁿

 array Properties of attachment.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/?output_format=md#source)󠁿

    ```php
    function wp_copy_parent_attachment_properties( $cropped, $parent_attachment_id, $context = '' ) {
    	$parent          = get_post( $parent_attachment_id );
    	$parent_url      = wp_get_attachment_url( $parent->ID );
    	$parent_basename = wp_basename( $parent_url );
    	$url             = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );

    	$size       = wp_getimagesize( $cropped );
    	$image_type = $size ? $size['mime'] : 'image/jpeg';

    	$sanitized_post_title = sanitize_file_name( $parent->post_title );
    	$use_original_title   = (
    		( '' !== trim( $parent->post_title ) ) &&
    		/*
    		 * Check if the original image has a title other than the "filename" default,
    		 * meaning the image had a title when originally uploaded or its title was edited.
    		 */
    		( $parent_basename !== $sanitized_post_title ) &&
    		( pathinfo( $parent_basename, PATHINFO_FILENAME ) !== $sanitized_post_title )
    	);
    	$use_original_description = ( '' !== trim( $parent->post_content ) );

    	$attachment = array(
    		'post_title'     => $use_original_title ? $parent->post_title : wp_basename( $cropped ),
    		'post_content'   => $use_original_description ? $parent->post_content : $url,
    		'post_mime_type' => $image_type,
    		'guid'           => $url,
    		'context'        => $context,
    	);

    	// Copy the image caption attribute (post_excerpt field) from the original image.
    	if ( '' !== trim( $parent->post_excerpt ) ) {
    		$attachment['post_excerpt'] = $parent->post_excerpt;
    	}

    	// Copy the image alt text attribute from the original image.
    	if ( '' !== trim( $parent->_wp_attachment_image_alt ) ) {
    		$attachment['meta_input'] = array(
    			'_wp_attachment_image_alt' => wp_slash( $parent->_wp_attachment_image_alt ),
    		);
    	}

    	$attachment['post_parent'] = $parent_attachment_id;

    	return $attachment;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/image.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-admin/includes/image.php#L522)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-admin/includes/image.php#L522-L566)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_getimagesize()](https://developer.wordpress.org/reference/functions/wp_getimagesize/)`wp-includes/media.php` |

Allows PHP’s getimagesize() to be debuggable when necessary.

  | 
| [sanitize_file_name()](https://developer.wordpress.org/reference/functions/sanitize_file_name/)`wp-includes/formatting.php` |

Sanitizes a filename, replacing whitespace with dashes.

  | 
| [wp_get_attachment_url()](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/)`wp-includes/post.php` |

Retrieves the URL for an attachment.

  | 
| [wp_basename()](https://developer.wordpress.org/reference/functions/wp_basename/)`wp-includes/formatting.php` |

i18n-friendly version of basename().

  | 
| [wp_slash()](https://developer.wordpress.org/reference/functions/wp_slash/)`wp-includes/formatting.php` |

Adds slashes to a string or recursively adds slashes to strings within an array.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/?output_format=md#)

| Used by | Description | 
| [wp_ajax_crop_image()](https://developer.wordpress.org/reference/functions/wp_ajax_crop_image/)`wp-admin/includes/ajax-actions.php` |

Handles cropping an image via AJAX.

  | 
| [Custom_Image_Header::ajax_header_crop()](https://developer.wordpress.org/reference/classes/custom_image_header/ajax_header_crop/)`wp-admin/includes/class-custom-image-header.php` |

Gets attachment uploaded by Media Manager, crops it, then saves it as a new object. Returns JSON-encoded object details.

  | 
| [Custom_Image_Header::step_3()](https://developer.wordpress.org/reference/classes/custom_image_header/step_3/)`wp-admin/includes/class-custom-image-header.php` |

Displays third step of custom header image page.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.5.0](https://developer.wordpress.org/reference/since/6.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_copy_parent_attachment_properties%2F)
before being able to contribute a note or feedback.