' . __( 'You can customize the look of your site without touching any of your theme’s code by using a custom background. Your background can be an image or a color.' ) . '
' .
'' . __( 'To use a background image, simply upload it or choose an image that has already been uploaded to your Media Library by clicking the “Choose Image” button. You can display a single instance of your image, or tile it to fill the screen. You can have your background fixed in place, so your site content moves on top of it, or you can have it scroll with your site.' ) . '
' .
'' . __( 'You can also choose a background color by clicking the Select Color button and either typing in a legitimate HTML hex value, e.g. “#ff0000” for red, or by choosing a color using the color picker.' ) . '
' .
'' . __( 'Do not forget to click on the Save Changes button when you are finished.' ) . '
',
)
);
get_current_screen()->set_help_sidebar(
'
Customizer.' ),
admin_url( 'customize.php?autofocus[control]=background_image' )
);
wp_admin_notice(
$message,
array(
'type' => 'info',
'additional_classes' => array( 'hide-if-no-customize' ),
)
);
}
if ( ! empty( $this->updated ) ) {
$updated_message = sprintf(
/* translators: %s: Home URL. */
__( 'Background updated.
Visit your site to see how it looks.' ),
esc_url( home_url( '/' ) )
);
wp_admin_notice(
$updated_message,
array(
'id' => 'message',
'additional_classes' => array( 'updated' ),
)
);
}
?>
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'] );
}
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$filename = wp_basename( $file );
// Construct the attachment array.
$attachment = array(
'post_title' => $filename,
'post_content' => $url,
'post_mime_type' => $type,
'guid' => $url,
'context' => 'custom-background',
);
// 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 filter is documented in wp-admin/includes/class-custom-image-header.php */
$file = apply_filters( '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
*/
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();
}
/**
* @since 3.4.0
* @deprecated 3.5.0
*
* @param array $form_fields
* @return array $form_fields
*/
public function attachment_fields_to_edit( $form_fields ) {
return $form_fields;
}
/**
* @since 3.4.0
* @deprecated 3.5.0
*
* @param array $tabs
* @return array $tabs
*/
public function filter_upload_tabs( $tabs ) {
return $tabs;
}
/**
* @since 3.4.0
* @deprecated 3.5.0
*/
public function wp_set_background_image() {
check_ajax_referer( 'custom-background' );
if ( ! current_user_can( 'edit_theme_options' ) || ! isset( $_POST['attachment_id'] ) ) {
exit;
}
$attachment_id = absint( $_POST['attachment_id'] );
$sizes = array_keys(
/** This filter is documented in wp-admin/includes/media.php */
apply_filters(
'image_size_names_choose',
array(
'thumbnail' => __( 'Thumbnail' ),
'medium' => __( 'Medium' ),
'large' => __( 'Large' ),
'full' => __( 'Full Size' ),
)
)
);
$size = 'thumbnail';
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] ) );
exit;
}
}