A WordPress-centric search engine for devs and theme authors



validate_plugin_requirements ›

Since5.2.0
Deprecatedn/a
validate_plugin_requirements ( $plugin )
Parameters:
  • (string) $plugin Path to the plugin file relative to the plugins directory.
    Required: Yes
Returns:
  • (bool|WP_Error) True if requirements are met, WP_Error on failure.
Defined at:
Codex:
Change Log:
  • 5.3.0

Validates the plugin requirements for WordPress version and PHP version.

Uses the information from Requires at least and Requires PHP headers defined in the plugin's main PHP file. If the headers are not present in the plugin's main PHP file, readme.txt is also checked as a fallback.


Source

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'] : '',
	);

	$readme_file = WP_PLUGIN_DIR . '/' . dirname( $plugin ) . '/readme.txt';

	if ( file_exists( $readme_file ) ) {
		$readme_headers = get_file_data(
			$readme_file,
			array(
				'requires'     => 'Requires at least',
				'requires_php' => 'Requires PHP',
			),
			'plugin'
		);

		$requirements = array_merge( $readme_headers, $requirements );
	}

	$compatible_wp  = is_wp_version_compatible( $requirements['requires'] );
	$compatible_php = is_php_version_compatible( $requirements['requires_php'] );

	if ( ! $compatible_wp && ! $compatible_php ) {
		return new WP_Error(
			'plugin_wp_php_incompatible',
			sprintf(
				/* translators: %s: Plugin name. */
				_x( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.', 'plugin' ),
				$plugin_headers['Name']
			)
		);
	} elseif ( ! $compatible_php ) {
		return new WP_Error(
			'plugin_php_incompatible',
			sprintf(
				/* translators: %s: Plugin name. */
				_x( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.', 'plugin' ),
				$plugin_headers['Name']
			)
		);
	} elseif ( ! $compatible_wp ) {
		return new WP_Error(
			'plugin_wp_incompatible',
			sprintf(
				/* translators: %s: Plugin name. */
				_x( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.', 'plugin' ),
				$plugin_headers['Name']
			)
		);
	}

	return true;
}