Theme_Upgrader::check_package( string $source )

Check that the package source contains a valid theme.


Description Description

Hooked to the ‘upgrader_source_selection’ filter by Theme_Upgrader::install(). It will return an error if the theme doesn’t have style.css or index.php files.


Parameters Parameters

$source

(string) (Required) The full path to the package source.


Top ↑

Return Return

(string|WP_Error) The source or a WP_Error.


Top ↑

Source Source

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

	public function check_package( $source ) {
		global $wp_filesystem;

		if ( is_wp_error($source) )
			return $source;

		// Check the folder contains a valid theme
		$working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
		if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation.
			return $source;

		// A proper archive should have a style.css file in the single subdirectory
		if ( ! file_exists( $working_directory . 'style.css' ) ) {
			return new WP_Error( 'incompatible_archive_theme_no_style', $this->strings['incompatible_archive'],
				/* translators: %s: style.css */
				sprintf( __( 'The theme is missing the %s stylesheet.' ),
					'<code>style.css</code>'
				)
			);
		}

		$info = get_file_data( $working_directory . 'style.css', array( 'Name' => 'Theme Name', 'Template' => 'Template' ) );

		if ( empty( $info['Name'] ) ) {
			return new WP_Error( 'incompatible_archive_theme_no_name', $this->strings['incompatible_archive'],
				/* translators: %s: style.css */
				sprintf( __( 'The %s stylesheet doesn&#8217;t contain a valid theme header.' ),
					'<code>style.css</code>'
				)
			);
		}

		// If it's not a child theme, it must have at least an index.php to be legit.
		if ( empty( $info['Template'] ) && ! file_exists( $working_directory . 'index.php' ) ) {
			return new WP_Error( 'incompatible_archive_theme_no_index', $this->strings['incompatible_archive'],
				/* translators: %s: index.php */
				sprintf( __( 'The theme is missing the %s file.' ),
					'<code>index.php</code>'
				)
			);
		}

		return $source;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.3.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.