validate_plugin_requirements

The timeline below displays how wordpress function validate_plugin_requirements has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Validates the plugin requirements for WordPress version and PHP version.
 *
 * Uses the information from `Requires at least`, `Requires PHP` and `Requires Plugins` headers
 * defined in the plugin's main PHP file.
 *
 * @since 5.2.0
 * @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.
 * @since 6.5.0 Added support for the 'Requires Plugins' header.
 *
 * @param string $plugin Path to the plugin file relative to the plugins directory.
 * @return true|WP_Error True if requirements are met, WP_Error on failure.
 */
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;
            }
        }
        $error_message = sprintf(
            /* translators: 1: Plugin name, 2: Number of plugins, 3: A comma-separated list of plugin names. */
            _n('<strong>Error:</strong> %1$s requires %2$d plugin to be installed and activated: %3$s.', '<strong>Error:</strong> %1$s requires %2$d plugins to be installed and activated: %3$s.', count($unmet_dependency_names)),
            $plugin_headers['Name'],
            count($unmet_dependency_names),
            implode(wp_get_list_item_separator(), $unmet_dependency_names)
        );
        if (is_multisite()) {
            if (current_user_can('manage_network_plugins')) {
                $error_message .= ' ' . sprintf(
                    /* translators: %s: Link to the plugins page. */
                    __('<a href="%s">Manage plugins</a>.'),
                    esc_url(network_admin_url('plugins.php'))
                );
            } else {
                $error_message .= ' ' . __('Please contact your network administrator.');
            }
        } else {
            $error_message .= ' ' . sprintf(
                /* translators: %s: Link to the plugins page. */
                __('<a href="%s">Manage plugins</a>.'),
                esc_url(admin_url('plugins.php'))
            );
        }
        return new WP_Error('plugin_missing_dependencies', "<p>{$error_message}</p>", $unmet_dependencies);
    }
    return true;
}

WordPress Version: 6.1

/**
 * 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.
 *
 * @since 5.2.0
 * @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.
 *
 * @param string $plugin Path to the plugin file relative to the plugins directory.
 * @return true|WP_Error True if requirements are met, WP_Error on failure.
 */
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;
}

WordPress Version: 5.8

/**
 * 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.
 *
 * @since 5.2.0
 * @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.
 *
 * @param string $plugin Path to the plugin file relative to the plugins directory.
 * @return true|WP_Error True if requirements are met, WP_Error on failure.
 */
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'),
            phpversion(),
            $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'),
            phpversion(),
            $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;
}

WordPress Version: 5.6

/**
 * 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.
 *
 * @since 5.2.0
 * @since 5.3.0 Added support for reading the headers from the plugin's
 *              main PHP file, with `readme.txt` as a fallback.
 *
 * @param string $plugin Path to the plugin file relative to the plugins directory.
 * @return true|WP_Error True if requirements are met, WP_Error on failure.
 */
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']);
    $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'),
            phpversion(),
            $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'),
            phpversion(),
            $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;
}

WordPress Version: 5.5

/**
 * 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.
 *
 * @since 5.2.0
 * @since 5.3.0 Added support for reading the headers from the plugin's
 *              main PHP file, with `readme.txt` as a fallback.
 *
 * @param string $plugin Path to the plugin file relative to the plugins directory.
 * @return true|WP_Error True if requirements are met, WP_Error on failure.
 */
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']);
    /* translators: %s: URL to Update PHP page. */
    $php_update_message = '</p><p>' . sprintf(__('<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'),
            phpversion(),
            $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'),
            phpversion(),
            $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;
}

WordPress Version: 5.3

/**
 * Validate the plugin requirements for WP version and PHP version.
 *
 * @since 5.2.0
 *
 * @param string $plugin Path to the plugin file relative to the plugins directory.
 * @return true|WP_Error True if requirements are met, WP_Error on failure.
 */
function validate_plugin_requirements($plugin)
{
    $readme_file = WP_PLUGIN_DIR . '/' . dirname($plugin) . '/readme.txt';
    $plugin_data = array('requires' => '', 'requires_php' => '');
    if (file_exists($readme_file)) {
        $plugin_data = get_file_data($readme_file, array('requires' => 'Requires at least', 'requires_php' => 'Requires PHP'), 'plugin');
    }
    $plugin_data = array_merge($plugin_data, get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin));
    // Check for headers in the plugin's PHP file, give precedence to the plugin headers.
    $plugin_data['requires'] = (!empty($plugin_data['RequiresWP'])) ? $plugin_data['RequiresWP'] : $plugin_data['requires'];
    $plugin_data['requires_php'] = (!empty($plugin_data['RequiresPHP'])) ? $plugin_data['RequiresPHP'] : $plugin_data['requires_php'];
    $plugin_data['wp_compatible'] = is_wp_version_compatible($plugin_data['requires']);
    $plugin_data['php_compatible'] = is_php_version_compatible($plugin_data['requires_php']);
    if (!$plugin_data['wp_compatible'] && !$plugin_data['php_compatible']) {
        return new WP_Error('plugin_wp_php_incompatible', sprintf(
            /* translators: %s: Plugin name. */
            __('<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.'),
            $plugin_data['Name']
        ));
    } elseif (!$plugin_data['php_compatible']) {
        return new WP_Error('plugin_php_incompatible', sprintf(
            /* translators: %s: Plugin name. */
            __('<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.'),
            $plugin_data['Name']
        ));
    } elseif (!$plugin_data['wp_compatible']) {
        return new WP_Error('plugin_wp_incompatible', sprintf(
            /* translators: %s: Plugin name. */
            __('<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.'),
            $plugin_data['Name']
        ));
    }
    return true;
}

WordPress Version: 5.2

/**
 * Validate the plugin requirements for WP version and PHP version.
 *
 * @since 5.2.0
 *
 * @param string $plugin Path to the plugin file relative to the plugins directory.
 * @return true|WP_Error True if requirements are met, WP_Error on failure.
 */
function validate_plugin_requirements($plugin)
{
    $readme_file = WP_PLUGIN_DIR . '/' . dirname($plugin) . '/readme.txt';
    if (file_exists($readme_file)) {
        $plugin_data = get_file_data($readme_file, array('requires' => 'Requires at least', 'requires_php' => 'Requires PHP'), 'plugin');
    } else {
        return true;
    }
    $plugin_data['wp_compatible'] = is_wp_version_compatible($plugin_data['requires']);
    $plugin_data['php_compatible'] = is_php_version_compatible($plugin_data['requires_php']);
    $plugin_data = array_merge($plugin_data, get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin));
    if (!$plugin_data['wp_compatible'] && !$plugin_data['php_compatible']) {
        return new WP_Error('plugin_wp_php_incompatible', sprintf(
            /* translators: %s: plugin name */
            __('<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.'),
            $plugin_data['Name']
        ));
    } elseif (!$plugin_data['php_compatible']) {
        return new WP_Error('plugin_php_incompatible', sprintf(
            /* translators: %s: plugin name */
            __('<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.'),
            $plugin_data['Name']
        ));
    } elseif (!$plugin_data['wp_compatible']) {
        return new WP_Error('plugin_wp_incompatible', sprintf(
            /* translators: %s: plugin name */
            __('<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.'),
            $plugin_data['Name']
        ));
    }
    return true;
}