On this page
Custom_Image_Header::ajax_header_crop()
Gets attachment uploaded by Media Manager, crops it, then saves it as a new object. Returns JSON-encoded object details.
Source
File: wp-admin/includes/class-custom-image-header.php. View all references
public function ajax_header_crop() {
check_ajax_referer( 'image_editor-' . $_POST['id'], 'nonce' );
if ( ! current_user_can( 'edit_theme_options' ) ) {
wp_send_json_error();
}
if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
wp_send_json_error();
}
$crop_details = $_POST['cropDetails'];
$dimensions = $this->get_header_dimensions(
array(
'height' => $crop_details['height'],
'width' => $crop_details['width'],
)
);
$attachment_id = absint( $_POST['id'] );
$cropped = wp_crop_image(
$attachment_id,
(int) $crop_details['x1'],
(int) $crop_details['y1'],
(int) $crop_details['width'],
(int) $crop_details['height'],
(int) $dimensions['dst_width'],
(int) $dimensions['dst_height']
);
if ( ! $cropped || is_wp_error( $cropped ) ) {
wp_send_json_error( array( 'message' => __( 'Image could not be processed. Please go back and try again.' ) ) );
}
/** This filter is documented in wp-admin/includes/class-custom-image-header.php */
$cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
$attachment = $this->create_attachment_object( $cropped, $attachment_id );
$previous = $this->get_previous_crop( $attachment );
if ( $previous ) {
$attachment['ID'] = $previous;
} else {
unset( $attachment['ID'] );
}
$new_attachment_id = $this->insert_attachment( $attachment, $cropped );
$attachment['attachment_id'] = $new_attachment_id;
$attachment['url'] = wp_get_attachment_url( $new_attachment_id );
$attachment['width'] = $dimensions['dst_width'];
$attachment['height'] = $dimensions['dst_height'];
wp_send_json_success( $attachment );
}
Hooks
- do_action( 'wp_create_file_in_uploads',
string $file ,int $attachment_id ) -
Fires after the header image is set or an error is returned.
Related
Uses
| Uses | Description |
|---|---|
| Custom_Image_Header::get_previous_crop() wp-admin/includes/class-custom-image-header.php | Get the ID of a previous crop from the same base image. |
| wp_crop_image() wp-admin/includes/image.php | Crops an image to a given size. |
| Custom_Image_Header::get_header_dimensions() wp-admin/includes/class-custom-image-header.php | Calculate width and height based on what the currently selected theme supports. |
| Custom_Image_Header::create_attachment_object() wp-admin/includes/class-custom-image-header.php | Create an attachment ‘object’. |
| Custom_Image_Header::insert_attachment() wp-admin/includes/class-custom-image-header.php | Insert an attachment and its metadata. |
| wp_get_attachment_url() wp-includes/post.php | Retrieves the URL for an attachment. |
| current_user_can() wp-includes/capabilities.php | Returns whether the current user has the specified capability. |
| current_theme_supports() wp-includes/theme.php | Checks a theme’s support for a given feature. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| check_ajax_referer() wp-includes/pluggable.php | Verifies the Ajax request to prevent processing requests external of the blog. |
| wp_send_json_error() wp-includes/functions.php | Sends a JSON response back to an Ajax request, indicating failure. |
| absint() wp-includes/functions.php | Converts a value to non-negative integer. |
| wp_send_json_success() wp-includes/functions.php | Sends a JSON response back to an Ajax request, indicating success. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
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/ajax_header_crop