validate_plugin_requirements()WP 5.2.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.

No Hooks.

Return

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

Usage

validate_plugin_requirements( $plugin );
$plugin(string) (required)
Path to the plugin file relative to the plugins directory.

Examples

0

#1 Checking plugin compatibility

Let's check the compatibility of PHP and WP Versions for Democracy Poll plugin:

To do this, the following code can be put on plugin activation hook:

register_activation_hook( __FILE__, 'myplugin_activate' );

function myplugin_activate(){

	// for the front
	require_once ABSPATH . '/wp-admin/includes/plugin.php'; 

	$plugin = 'democracy-poll/democracy.php';
	$valid  = validate_plugin_requirements( $plugin );

	if( is_wp_error( $valid ) ){

		wp_die( $valid->get_error_message() );
		// Will lead out:
		// Error: Current WordPress and PHP versions do not meet minimum requirements for Democracy Poll.

	}

}

Changelog

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

validate_plugin_requirements() code WP 6.4.3

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

	$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>'
		);
	}

	return true;
}