WP_REST_Attachments_Controller::prepare_item_for_database() WordPress Method

The WP_REST_Attachments_Controller::prepare_item_for_database() method is used to prepare an attachment for insertion into the database. This method can be used to set the attachment title, caption, description, and alt text.

WP_REST_Attachments_Controller::prepare_item_for_database( WP_REST_Request $request ) #

Prepares a single attachment for create or update.


Parameters

$request

(WP_REST_Request)(Required)Request object.


Top ↑

Return

(stdClass|WP_Error) Post object.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

	protected function prepare_item_for_database( $request ) {
		$prepared_attachment = parent::prepare_item_for_database( $request );

		// Attachment caption (post_excerpt internally).
		if ( isset( $request['caption'] ) ) {
			if ( is_string( $request['caption'] ) ) {
				$prepared_attachment->post_excerpt = $request['caption'];
			} elseif ( isset( $request['caption']['raw'] ) ) {
				$prepared_attachment->post_excerpt = $request['caption']['raw'];
			}
		}

		// Attachment description (post_content internally).
		if ( isset( $request['description'] ) ) {
			if ( is_string( $request['description'] ) ) {
				$prepared_attachment->post_content = $request['description'];
			} elseif ( isset( $request['description']['raw'] ) ) {
				$prepared_attachment->post_content = $request['description']['raw'];
			}
		}

		if ( isset( $request['post'] ) ) {
			$prepared_attachment->post_parent = (int) $request['post'];
		}

		return $prepared_attachment;
	}


Top ↑

Changelog

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