Custom_Image_Header::set_header_image() WordPress Method

This function sets the header image for a WordPress theme. The image should be stored in the theme's "images" folder. The function takes the path to the image as its only parameter. The function returns an array of the image's URL and width.

Custom_Image_Header::set_header_image( mixed $choice ) #

Choose a header image, selected from existing uploaded and default headers, or provide an array of uploaded header data (either new, or from media library).


Parameters

$choice

(mixed)(Required)Which header image to select. Allows for values of 'random-default-image', for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling among the uploaded images; the key of a default image registered for that theme; and the key of an image uploaded for that theme (the attachment ID of the image). Or an array of arguments: attachment_id, url, width, height. All are required.


Top ↑

Source

File: wp-admin/includes/class-custom-image-header.php

	final public function set_header_image( $choice ) {
		if ( is_array( $choice ) || is_object( $choice ) ) {
			$choice = (array) $choice;

			if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) {
				return;
			}

			$choice['url'] = esc_url_raw( $choice['url'] );

			$header_image_data = (object) array(
				'attachment_id' => $choice['attachment_id'],
				'url'           => $choice['url'],
				'thumbnail_url' => $choice['url'],
				'height'        => $choice['height'],
				'width'         => $choice['width'],
			);

			update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );

			set_theme_mod( 'header_image', $choice['url'] );
			set_theme_mod( 'header_image_data', $header_image_data );

			return;
		}

		if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ), true ) ) {
			set_theme_mod( 'header_image', $choice );
			remove_theme_mod( 'header_image_data' );

			return;
		}

		$uploaded = get_uploaded_header_images();

		if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
			$header_image_data = $uploaded[ $choice ];
		} else {
			$this->process_default_headers();
			if ( isset( $this->default_headers[ $choice ] ) ) {
				$header_image_data = $this->default_headers[ $choice ];
			} else {
				return;
			}
		}

		set_theme_mod( 'header_image', esc_url_raw( $header_image_data['url'] ) );
		set_theme_mod( 'header_image_data', $header_image_data );
	}


Top ↑

Changelog

Changelog
VersionDescription
3.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.