WP_Theme::get_screenshot() WordPress Method

The WP_Theme::get_screenshot() function returns the URL for the screenshot of the theme. The screenshot is determined by the presence of a screenshot.png file in the theme's directory. If the file is not present, the function returns the URL of the default screenshot.

WP_Theme::get_screenshot( string $uri = 'uri' ) #

Returns the main screenshot file for the theme.


Description

The main screenshot is called screenshot.png. gif and jpg extensions are also allowed.

Screenshots for a theme must be in the stylesheet directory. (In the case of child themes, parent theme screenshots are not inherited.)


Top ↑

Parameters

$uri

(string)(Optional)Type of URL to return, either 'relative' or an absolute URI. Defaults to absolute URI.

Default value: 'uri'


Top ↑

Return

(string|false) Screenshot file. False if the theme does not have a screenshot.


Top ↑

Source

File: wp-includes/class-wp-theme.php

	public function get_screenshot( $uri = 'uri' ) {
		$screenshot = $this->cache_get( 'screenshot' );
		if ( $screenshot ) {
			if ( 'relative' === $uri ) {
				return $screenshot;
			}
			return $this->get_stylesheet_directory_uri() . '/' . $screenshot;
		} elseif ( 0 === $screenshot ) {
			return false;
		}

		foreach ( array( 'png', 'gif', 'jpg', 'jpeg', 'webp' ) as $ext ) {
			if ( file_exists( $this->get_stylesheet_directory() . "/screenshot.$ext" ) ) {
				$this->cache_add( 'screenshot', 'screenshot.' . $ext );
				if ( 'relative' === $uri ) {
					return 'screenshot.' . $ext;
				}
				return $this->get_stylesheet_directory_uri() . '/' . 'screenshot.' . $ext;
			}
		}

		$this->cache_add( 'screenshot', 0 );
		return false;
	}


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.