: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
/* translators: Hidden accessibility text. */
_e( 'Background Color' );
if ( current_theme_supports( 'custom-background', 'default-color' ) ) {
$default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
<input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color; ?>>
<?php wp_nonce_field( 'custom-background' ); ?>
<?php submit_button( null, 'primary', 'save-background-options' ); ?>
* Handles an Image upload for the background image.
public function handle_upload() {
if ( empty( $_FILES ) ) {
check_admin_referer( 'custom-background-upload', '_wpnonce-custom-background-upload' );
$overrides = array( 'test_form' => false );
$uploaded_file = $_FILES['import'];
$wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
$file = wp_handle_upload( $uploaded_file, $overrides );
if ( isset( $file['error'] ) ) {
wp_die( $file['error'] );
$filename = wp_basename( $file );
// Construct the attachment array.
'post_title' => $filename,
'post_mime_type' => $type,
'context' => 'custom-background',
$id = wp_insert_attachment( $attachment, $file );
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 filter is documented in wp-admin/includes/class-custom-image-header.php */
$file = apply_filters( 'wp_create_file_in_uploads', $file, $id ); // For replication.
* Handles Ajax request for adding custom background context to an attachment.
* Triggers when the user adds a new background image from the
public function ajax_background_add() {
check_ajax_referer( 'background-add', 'nonce' );
if ( ! current_user_can( 'edit_theme_options' ) ) {
$attachment_id = absint( $_POST['attachment_id'] );
if ( $attachment_id < 1 ) {
update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_stylesheet() );
* @param array $form_fields
* @return array $form_fields
public function attachment_fields_to_edit( $form_fields ) {
public function filter_upload_tabs( $tabs ) {
public function wp_set_background_image() {
check_ajax_referer( 'custom-background' );
if ( ! current_user_can( 'edit_theme_options' ) || ! isset( $_POST['attachment_id'] ) ) {
$attachment_id = absint( $_POST['attachment_id'] );
/** This filter is documented in wp-admin/includes/media.php */
'image_size_names_choose',
'thumbnail' => __( 'Thumbnail' ),
'medium' => __( 'Medium' ),
'large' => __( 'Large' ),
'full' => __( 'Full Size' ),
if ( in_array( $_POST['size'], $sizes, true ) ) {
$size = esc_attr( $_POST['size'] );
update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
$url = wp_get_attachment_image_src( $attachment_id, $size );
$thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
set_theme_mod( 'background_image', sanitize_url( $url[0] ) );
set_theme_mod( 'background_image_thumb', sanitize_url( $thumbnail[0] ) );