Title: Custom_Image_Header::set_header_image
Published: April 25, 2014
Last modified: August 20, 2026

---

# Custom_Image_Header::set_header_image( mixed $choice )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/custom_image_header/set_header_image/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/classes/custom_image_header/set_header_image/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/custom_image_header/set_header_image/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/custom_image_header/set_header_image/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/custom_image_header/set_header_image/?output_format=md#wp--skip-link--target)

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

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/custom_image_header/set_header_image/?output_format=md#parameters)󠁿

 `$choice`mixedrequired

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.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/custom_image_header/set_header_image/?output_format=md#source)󠁿

    ```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'] = sanitize_url( $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', sanitize_url( $header_image_data['url'] ) );
    	set_theme_mod( 'header_image_data', $header_image_data );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/class-custom-image-header.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-admin/includes/class-custom-image-header.php#L1173)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-admin/includes/class-custom-image-header.php#L1173-L1221)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/custom_image_header/set_header_image/?output_format=md#related)󠁿

| Uses | Description | 
| [Custom_Image_Header::process_default_headers()](https://developer.wordpress.org/reference/classes/custom_image_header/process_default_headers/)`wp-admin/includes/class-custom-image-header.php` |

Processes the default headers.

  | 
| [set_theme_mod()](https://developer.wordpress.org/reference/functions/set_theme_mod/)`wp-includes/theme.php` |

Updates theme modification value for the active theme.

  | 
| [remove_theme_mod()](https://developer.wordpress.org/reference/functions/remove_theme_mod/)`wp-includes/theme.php` |

Removes theme modification name from active theme list.

  | 
| [get_uploaded_header_images()](https://developer.wordpress.org/reference/functions/get_uploaded_header_images/)`wp-includes/theme.php` |

Gets the header images uploaded for the active theme.

  | 
| [get_stylesheet()](https://developer.wordpress.org/reference/functions/get_stylesheet/)`wp-includes/theme.php` |

Retrieves name of the current stylesheet.

  | 
| [update_post_meta()](https://developer.wordpress.org/reference/functions/update_post_meta/)`wp-includes/post.php` |

Updates a post meta field based on the given post ID.

  | 
| [sanitize_url()](https://developer.wordpress.org/reference/functions/sanitize_url/)`wp-includes/formatting.php` |

Sanitizes a URL for database or redirect usage.

  |

[Show 2 more](https://developer.wordpress.org/reference/classes/custom_image_header/set_header_image/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/custom_image_header/set_header_image/?output_format=md#)

| Used by | Description | 
| [Custom_Image_Header::step_2()](https://developer.wordpress.org/reference/classes/custom_image_header/step_2/)`wp-admin/includes/class-custom-image-header.php` |

Displays second step of custom header image page.

  | 
| [Custom_Image_Header::step_3()](https://developer.wordpress.org/reference/classes/custom_image_header/step_3/)`wp-admin/includes/class-custom-image-header.php` |

Displays third step of custom header image page.

  | 
| [Custom_Image_Header::remove_header_image()](https://developer.wordpress.org/reference/classes/custom_image_header/remove_header_image/)`wp-admin/includes/class-custom-image-header.php` |

Removes a header image.

  | 
| [Custom_Image_Header::take_action()](https://developer.wordpress.org/reference/classes/custom_image_header/take_action/)`wp-admin/includes/class-custom-image-header.php` |

Executes custom header modification.

  | 
| [WP_Customize_Header_Image_Setting::update()](https://developer.wordpress.org/reference/classes/wp_customize_header_image_setting/update/)`wp-includes/customize/class-wp-customize-header-image-setting.php` |  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/custom_image_header/set_header_image/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.4.0](https://developer.wordpress.org/reference/since/3.4.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fcustom_image_header%2Fset_header_image%2F)
before being able to contribute a note or feedback.