wppaste
WordPress

validate_plugin_requirements( string $plugin ): true|WP_Error

Since
5.2.0, 5.3.0, 5.8.0, 6.5.0
Source
wp-admin/includes/plugin.php:1139
Validates the plugin requirements for WordPress version and PHP version.

Description

Uses the information from Requires at least, Requires PHP and Requires Plugins headers defined in the plugin's main PHP file.

Compatibility

WordPress
since 6.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$pluginstring
Path to the plugin file relative to the plugins directory.

Return value

true|WP_Error
True if requirements are met, WP_Error on failure.

Performance profile

How much work a call to validate_plugin_requirements() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
65–124

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 246.

Plugin surface
1 hook

Third-party callbacks on 'validate_plugin_requirements' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()one call below validate_plugin_requirements()

Further down the call graph this can also reach cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 7 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost validate_plugin_requirements() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!::WP_Plugin_Dependencies()65–71get_plugin_data(), is_wp_version_compatible(), is_php_version_compatible(), __(), wp_get_update_php_url(), esc_url(), sprintf(), wp_get_update_php_annotation(), ::WP_Plugin_Dependencies(), ::WP_Plugin_Dependencies(), apply_filters()
always72–78get_plugin_data(), is_wp_version_compatible(), is_php_version_compatible(), __(), wp_get_update_php_url(), esc_url(), sprintf(), wp_get_update_php_annotation(), _x(), sprintf()
always75–81get_plugin_data(), is_wp_version_compatible(), is_php_version_compatible(), __(), wp_get_update_php_url(), esc_url(), sprintf(), wp_get_update_php_annotation(), _x(), get_bloginfo(), sprintf()
always78–84get_plugin_data(), is_wp_version_compatible(), is_php_version_compatible(), __(), wp_get_update_php_url(), esc_url(), sprintf(), wp_get_update_php_annotation(), _x(), get_bloginfo(), sprintf(), requires()
::WP_Plugin_Dependencies() && is_multisite() && !current_user_can()107–114get_plugin_data(), is_wp_version_compatible(), is_php_version_compatible(), __(), wp_get_update_php_url(), esc_url(), sprintf(), wp_get_update_php_annotation(), ::WP_Plugin_Dependencies(), ::WP_Plugin_Dependencies(), ::WP_Plugin_Dependencies(), _n(), wp_get_list_item_separator(), sprintf(), is_multisite(), current_user_can(), __()
::WP_Plugin_Dependencies() && !is_multisite()112–119get_plugin_data(), is_wp_version_compatible(), is_php_version_compatible(), __(), wp_get_update_php_url(), esc_url(), sprintf(), wp_get_update_php_annotation(), ::WP_Plugin_Dependencies(), ::WP_Plugin_Dependencies(), ::WP_Plugin_Dependencies(), _n(), wp_get_list_item_separator(), sprintf(), is_multisite(), __(), admin_url(), esc_url(), sprintf()
::WP_Plugin_Dependencies() && is_multisite() && current_user_can()117–124get_plugin_data(), is_wp_version_compatible(), is_php_version_compatible(), __(), wp_get_update_php_url(), esc_url(), sprintf(), wp_get_update_php_annotation(), ::WP_Plugin_Dependencies(), ::WP_Plugin_Dependencies(), ::WP_Plugin_Dependencies(), _n(), wp_get_list_item_separator(), sprintf(), is_multisite(), current_user_can(), __(), network_admin_url(), esc_url(), sprintf()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev24665–12415
8.524665–12415
8.424665–124153 fewer instructions than PHP 8.3
8.324965–12715
8.224965–12715
8.124965–127153 fewer instructions than PHP 7.4
7.425265–13015

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while validate_plugin_requirements() runs, in this order:

  1. apply_filters( validate_plugin_requirements )filterline 1269 (+130 into the body)

    Filters the plugin requirement validation response.

Uses · 22

Show all 22

Used by · 1

  • activate_plugin()Attempts activation of plugin in a "sandbox" and redirects on success.

Source code

function validate_plugin_requirements( $plugin ) {	$plugin_headers = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); 	$requirements = array(		'requires'         => ! empty( $plugin_headers['RequiresWP'] ) ? $plugin_headers['RequiresWP'] : '',		'requires_php'     => ! empty( $plugin_headers['RequiresPHP'] ) ? $plugin_headers['RequiresPHP'] : '',		'requires_plugins' => ! empty( $plugin_headers['RequiresPlugins'] ) ? $plugin_headers['RequiresPlugins'] : '',	); 	$compatible_wp  = is_wp_version_compatible( $requirements['requires'] );	$compatible_php = is_php_version_compatible( $requirements['requires_php'] ); 	$php_update_message = '</p><p>' . sprintf(		/* translators: %s: URL to Update PHP page. */		__( '<a href="%s">Learn more about updating PHP</a>.' ),		esc_url( wp_get_update_php_url() )	); 	$annotation = wp_get_update_php_annotation(); 	if ( $annotation ) {		$php_update_message .= '</p><p><em>' . $annotation . '</em>';	} 	if ( ! $compatible_wp && ! $compatible_php ) {		return new WP_Error(			'plugin_wp_php_incompatible',			'<p>' . sprintf(				/* translators: 1: Current WordPress version, 2: Current PHP version, 3: Plugin name, 4: Required WordPress version, 5: Required PHP version. */				_x( '<strong>Error:</strong> Current versions of WordPress (%1$s) and PHP (%2$s) do not meet minimum requirements for %3$s. The plugin requires WordPress %4$s and PHP %5$s.', 'plugin' ),				get_bloginfo( 'version' ),				PHP_VERSION,				$plugin_headers['Name'],				$requirements['requires'],				$requirements['requires_php']			) . $php_update_message . '</p>'		);	} elseif ( ! $compatible_php ) {		return new WP_Error(			'plugin_php_incompatible',			'<p>' . sprintf(				/* translators: 1: Current PHP version, 2: Plugin name, 3: Required PHP version. */				_x( '<strong>Error:</strong> Current PHP version (%1$s) does not meet minimum requirements for %2$s. The plugin requires PHP %3$s.', 'plugin' ),				PHP_VERSION,				$plugin_headers['Name'],				$requirements['requires_php']			) . $php_update_message . '</p>'		);	} elseif ( ! $compatible_wp ) {		return new WP_Error(			'plugin_wp_incompatible',			'<p>' . sprintf(				/* translators: 1: Current WordPress version, 2: Plugin name, 3: Required WordPress version. */				_x( '<strong>Error:</strong> Current WordPress version (%1$s) does not meet minimum requirements for %2$s. The plugin requires WordPress %3$s.', 'plugin' ),				get_bloginfo( 'version' ),				$plugin_headers['Name'],				$requirements['requires']			) . '</p>'		);	} 	WP_Plugin_Dependencies::initialize(); 	if ( WP_Plugin_Dependencies::has_unmet_dependencies( $plugin ) ) {		$dependency_names       = WP_Plugin_Dependencies::get_dependency_names( $plugin );		$unmet_dependencies     = array();		$unmet_dependency_names = array(); 		foreach ( $dependency_names as $dependency => $dependency_name ) {			$dependency_file = WP_Plugin_Dependencies::get_dependency_filepath( $dependency ); 			if ( false === $dependency_file ) {				$unmet_dependencies['not_installed'][ $dependency ] = $dependency_name;				$unmet_dependency_names[]                           = $dependency_name;			} elseif ( is_plugin_inactive( $dependency_file ) ) {				$unmet_dependencies['inactive'][ $dependency ] = $dependency_name;				$unmet_dependency_names[]                      = $dependency_name;			}		}

Changelog

Introduced in 5.2.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.5.0
Added support for the 'Requires Plugins' header.from the docblock
5.8.0
Removed support for using readme.txt as a fallback.from the docblock
5.3.0
Added support for reading the headers from the plugin's main PHP file, with readme.txt as a fallback.from the docblock
5.2.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-admin/includes/plugin.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.