On this page
Custom_Background::ajax_background_add()
Handles Ajax request for adding custom background context to an attachment.
Description
Triggers when the user adds a new background image from the Media Manager.
Source
File: wp-admin/includes/class-custom-background.php. View all references
public function ajax_background_add() {
check_ajax_referer( 'background-add', 'nonce' );
if ( ! current_user_can( 'edit_theme_options' ) ) {
wp_send_json_error();
}
$attachment_id = absint( $_POST['attachment_id'] );
if ( $attachment_id < 1 ) {
wp_send_json_error();
}
update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_stylesheet() );
wp_send_json_success();
}
Related
Uses
| Uses | Description |
|---|---|
| update_post_meta() wp-includes/post.php | Updates a post meta field based on the given post ID. |
| get_stylesheet() wp-includes/theme.php | Retrieves name of the current stylesheet. |
| current_user_can() wp-includes/capabilities.php | Returns whether the current user has the specified capability. |
| 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. |
Changelog
| Version | Description |
|---|---|
| 4.1.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/custom_background/ajax_background_add