WP_Site_Icon::create_attachment_object() WordPress Method

The WP_Site_Icon::create_attachment_object() function can be used to create an attachment post object for a site icon. This is useful for creating custom site icon sizes, or for creating an attachment object for an icon that already exists (such as when the icon is uploaded from the WordPress media library).

WP_Site_Icon::create_attachment_object( string $cropped, int $parent_attachment_id ) #

Creates an attachment ‘object’.


Parameters

$cropped

(string)(Required)Cropped image URL.

$parent_attachment_id

(int)(Required)Attachment ID of parent image.


Top ↑

Return

(array) An array with attachment object data.


Top ↑

Source

File: wp-admin/includes/class-wp-site-icon.php

	public function create_attachment_object( $cropped, $parent_attachment_id ) {
		$parent     = get_post( $parent_attachment_id );
		$parent_url = wp_get_attachment_url( $parent->ID );
		$url        = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );

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

		$attachment = array(
			'ID'             => $parent_attachment_id,
			'post_title'     => wp_basename( $cropped ),
			'post_content'   => $url,
			'post_mime_type' => $image_type,
			'guid'           => $url,
			'context'        => 'site-icon',
		);

		return $attachment;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.3.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.