WP_REST_Attachments_Controller
- Since
- 4.7.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:27
Core controller used to access attachments via the REST API.
Compatibility
- WordPress
- since 4.7.0
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0).
Hooks and filters fired · 15
Every hook that fires from inside WP_REST_Attachments_Controller, in the order it appears in the class, grouped by the method that fires it.
WP_REST_Attachments_Controller::create_item_permissions_check()
- apply_filters( wp_prevent_unsupported_mime_type_uploads )filter line 440
WP_REST_Attachments_Controller::create_item()
- do_action( rest_after_insert_attachment )action line 610
WP_REST_Attachments_Controller::create_item_from_url()
- do_action( rest_after_insert_attachment )action line 785
WP_REST_Attachments_Controller::insert_attachment()
- do_action( rest_insert_attachment )action line 918
WP_REST_Attachments_Controller::update_item()
- do_action( rest_after_insert_attachment )action line 1013
WP_REST_Attachments_Controller::edit_media_item()
- apply_filters( wp_edited_image_metadata )filter line 1368
WP_REST_Attachments_Controller::prepare_item_for_response()
- apply_filters( the_content )filter line 1438
- apply_filters( get_the_excerpt )filter line 1444
- apply_filters( the_excerpt )filter line 1447
- apply_filters( fallback_intermediate_image_sizes )filter line 1540
- apply_filters( image_editor_output_format )filter line 1587
- apply_filters( image_save_progressive )filter line 1604
- apply_filters( image_editor_output_format )filter line 1611
- apply_filters( rest_prepare_attachment )filter line 1681
Properties · 1
$allow_batchfalseprotected- Whether the controller supports batching.
Methods · 38
- register_routes()Registers the routes for attachments.
- get_endpoint_args_for_item_schema()Retrieves the query params for the attachments collection.
- prepare_items_query()Determines the allowed query_vars for a get_items() response and prepares for WP_Query.
- create_item_permissions_check()Checks if a given request has access to create an attachment.
- create_item()Creates a single attachment.
- create_item_from_url()Sideloads an external image from a URL into the media library.
- remove_client_side_media_processing_filters()Removes filters added for client-side media processing.
- insert_attachment()Inserts the attachment post in the database. Does not update the attachment meta.
- handle_featured_media()Determines the featured media based on a request param.
- update_item()Updates a single attachment.
- post_process_item()Performs post-processing on an attachment.
- post_process_item_permissions_check()Checks if a given request can perform post-processing on an attachment.
- edit_media_item_permissions_check()Checks if a given request has access to editing media.
- edit_media_item()Applies edits to a media item and creates a new attachment record.
- prepare_item_for_database()Prepares a single attachment for create or update.
- prepare_item_for_response()Prepares a single attachment output for response.
- prepare_links()Prepares attachment links for the request.
- get_item_schema()Retrieves the attachment's schema, conforming to JSON Schema.
- upload_from_data()Handles an upload via raw POST data.
- get_filename_from_disposition()Parses filename from a Content-Disposition header value.
- get_collection_params()Retrieves the query params for collections of attachments.
- upload_from_file()Handles an upload via multipart/form-data ($_FILES).
- get_media_types()Retrieves the supported media types.
- check_upload_size()Determine if uploaded file exceeds space quota on multisite.
- get_edit_media_item_args()Gets the request args for the edit item route.
- get_attachment_filename()Gets the attachment's original file name.
- get_attachment_filesize()Gets the attachment's file size in bytes.
- sideload_item_permissions_check()Checks if a given request has access to sideload a file.
- validate_image_size_names()Validates an image size name, or an array of names sharing a single file.
- get_special_image_sizes()Returns the image size names which name a single file rather than a sub-size.
- validate_image_dimensions()Validates that uploaded image dimensions are appropriate for the specified image size.
- dimension_exceeds_max()Checks whether a dimension exceeds the maximum allowed value.
- sideload_item()Side-loads a media file without creating a new attachment.
- filter_wp_unique_filename()Filters wp_unique_filename during sideloads.
- validate_sub_size_provenance()Validates the `sub_sizes` file names against what this attachment produced.
- get_sideloaded_file_names()Returns the file names which a finalize request may store for an attachment.
- get_attachment_upload_subdir()Returns the uploads subdirectory an attachment is stored in.
- finalize_item()Finalizes an attachment after client-side media processing.
Source code
class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { /** * Whether the controller supports batching. * * @since 5.9.0 * @var false */ protected $allow_batch = false; /** * Image size token for the source-format original preserved alongside a * client-generated derivative (e.g. the HEIC file kept next to its JPEG). * * Used both in the `/sideload` route schema and when dispatching the * sideloaded file to its metadata key, so the two never drift apart. * * @since 7.1.0 * @var string */ const IMAGE_SIZE_SOURCE_ORIGINAL = 'source_original'; /** * Metadata key holding the basename of the source-format original. * * Deliberately specific so it never collides with the generic `original` * or `original_image` keys other flows write to. * * @since 7.1.0 * @var string */ const META_KEY_SOURCE_IMAGE = 'source_image'; /** * Post meta key recording the file names produced by the sideload endpoint. * * Each successful sideload appends the file name(s) it created for an * attachment under this key. The finalize endpoint reads them back to * confirm every stored sub-size was actually produced here, rather than * trusting a client-supplied name that could point at another attachment's * files. Stored as one row per value (via {@see add_post_meta()}) so concurrent * sideloads never read-modify-write a shared value. * * @since 7.1.0 * @var string */ const META_KEY_SIDELOAD_FILE_NAME = '_wp_sideloaded_file'; /** * Registers the routes for attachments. * * @since 5.3.0 * * @see register_rest_route() */ public function register_routes() { parent::register_routes(); register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/post-process', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'post_process_item' ), 'permission_callback' => array( $this, 'post_process_item_permissions_check' ), 'args' => array( 'id' => array( 'description' => __( 'Unique identifier for the attachment.' ), 'type' => 'integer', ), 'action' => array( 'type' => 'string', 'enum' => array( 'create-image-subsizes' ), 'required' => true, ), ), ) ); register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/edit',Changelog
Introduced in 4.7.0. 17 changes between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
7.1.0
Method
get_endpoint_args_for_item_schema() added.verified against source7.1.0
Method
create_item_from_url() added.verified against source7.1.0
Method
remove_client_side_media_processing_filters() added.verified against source7.1.0
Method
sideload_item_permissions_check() added.verified against source7.1.0
Method
validate_image_size_names() added.verified against source7.1.0
Method
get_special_image_sizes() added.verified against source7.1.0
Method
validate_image_dimensions() added.verified against source7.1.0
Method
dimension_exceeds_max() added.verified against source7.1.0
Method
sideload_item() added.verified against source7.1.0
Method
filter_wp_unique_filename() added.verified against source7.1.0
Method
validate_sub_size_provenance() added.verified against source7.1.0
Method
get_sideloaded_file_names() added.verified against source7.1.0
Method
get_attachment_upload_subdir() added.verified against source7.1.0
Method
finalize_item() added.verified against source7.0.4
Method
get_attachment_filename() added.verified against source7.0.4
Method
get_attachment_filesize() added.verified against source6.9.7
Method
prepare_links() added.verified against source4.7.0
Introduced.from the docblock
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.