+ /**
+ * Replace default attachment actions with "Set as background" link.
+ *
+ * @since 3.4.0
+ */
+ function attachment_fields_to_edit( $form_fields, $post ) {
+ $form_fields = array( 'image-size' => $form_fields['image-size'] );
+ $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td><a data-attachment-id="' . $post->ID . '" class="wp-set-background">' . __( 'Set as background' ) . '</a></td></tr>' );
+ $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' );
+
+ return $form_fields;
+ }
+
+ /**
+ * Leave only "Media Library" tab in the uploader window.
+ *
+ * @since 3.4.0
+ */
+ function filter_upload_tabs() {
+ return array( 'library' => __('Media Library') );
+ }
+
+ public function wp_set_background_image() {
+ if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
+ $attachment_id = absint($_POST['attachment_id']);
+ $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
+ $size = 'thumbnail';
+ if ( in_array( $_POST['size'], $sizes ) )
+ $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', esc_url_raw( $url[0] ) );
+ set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
+ exit;
+ }