Custom_Image_Header::insert_attachment() WordPress Method

The Custom_Image_Header::insert_attachment() method is used to insert an attachment into a WordPress post. The method takes two parameters: the post ID of the post to which the attachment should be added, and the attachment ID.

Custom_Image_Header::insert_attachment( array $attachment, string $cropped ) #

Insert an attachment and its metadata.


Parameters

$attachment

(array)(Required)An array with attachment object data.

$cropped

(string)(Required)File path to cropped image.


Top ↑

Return

(int) Attachment ID.


Top ↑

Source

File: wp-admin/includes/class-custom-image-header.php

	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;
	}


Top ↑

Changelog

Changelog
VersionDescription
3.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.