// Save the data.
$id =
wp_insert_attachment( $attachment,
$file );
// Add the metadata.
wp_update_attachment_metadata( $id,
wp_generate_attachment_metadata( $id,
$file ) );
update_post_meta( $id, '_wp_attachment_is_custom_background',
get_option( 'stylesheet'
) );
set_theme_mod( 'background_image',
sanitize_url( $url ) );
$thumbnail =
wp_get_attachment_image_src( $id, 'thumbnail'
);
set_theme_mod( 'background_image_thumb',
sanitize_url( $thumbnail[0
] ) );
/** This action is documented in wp-admin/includes/class-custom-image-header.php */
do_action( 'wp_create_file_in_uploads',
$file,
$id ); // For replication.
$this->updated = true;
} /**
* Handles Ajax request for adding custom background context to an attachment.
*
* Triggers when the user adds a new background image from the
* Media Manager.
*
* @since 4.1.0
*/