Custom_Background::wp_set_background_image()publicWP 3.4.0

Deprecated from version 3.5.0. It is no longer supported and can be removed in future releases. It is recommended to replace this function with the same one.

Method of the class: Custom_Background{}

Hooks from the method

Return

null. Nothing.

Usage

$Custom_Background = new Custom_Background();
$Custom_Background->wp_set_background_image();

Changelog

Since 3.4.0 Introduced.
Deprecated since 3.5.0

Custom_Background::wp_set_background_image() code WP 6.1.1

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;
}