On this page
Custom_Image_Header::insert_attachment( array $attachment, string $cropped ): int
Insert an attachment and its metadata.
Parameters
$attachmentarray Required-
An array with attachment object data.
$croppedstring Required-
File path to cropped image.
Return
int Attachment ID.
Source
File: wp-admin/includes/class-custom-image-header.php. View all references
final public function insert_attachment( $attachment, $cropped ) {
$parent_id = isset( $attachment['post_parent'] ) ? $attachment['post_parent'] : null;
unset( $attachment['post_parent'] );
$attachment_id = wp_insert_attachment( $attachment, $cropped );
$metadata = wp_generate_attachment_metadata( $attachment_id, $cropped );
// If this is a crop, save the original attachment ID as metadata.
if ( $parent_id ) {
$metadata['attachment_parent'] = $parent_id;
}
/**
* Filters the header image attachment metadata.
*
* @since 3.9.0
*
* @see wp_generate_attachment_metadata()
*
* @param array $metadata Attachment metadata.
*/
$metadata = apply_filters( 'wp_header_image_attachment_metadata', $metadata );
wp_update_attachment_metadata( $attachment_id, $metadata );
return $attachment_id;
}
Hooks
- apply_filters( 'wp_header_image_attachment_metadata',
array $metadata ) -
Filters the header image attachment metadata.
Related
Uses
| Uses | Description |
|---|---|
| wp_generate_attachment_metadata() wp-admin/includes/image.php | Generates attachment meta data and create image sub-sizes for images. |
| wp_insert_attachment() wp-includes/post.php | Inserts an attachment. |
| wp_update_attachment_metadata() wp-includes/post.php | Updates metadata for an attachment. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| 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() wp-admin/includes/class-custom-image-header.php | Display third step of custom header image page. |
Changelog
| Version | Description |
|---|---|
| 3.9.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/custom_image_header/insert_attachment