Skip to content

Requirements configuration

The requirements service is used to define the requirements of your plugin. Setting requirements allows you to specify the minimum PHP version, WordPress version, and other dependencies that your plugin needs to function properly.

When the requirements are not met, the plugin will display an admin notice with a message informing the user about the missing requirements and the plugin is then deactivated.

TIP

The service is automatically registered with the container and can be accessed by injecting it into your classes. The service is an instance of the Requirements class.

Setting requirements

To set the requirements of your plugin, you must set the requirements array in the plugin's configuration file. The array should contain instances of the Sematico\Metapress\Requirements\Checks classes.

php
return [
    'requirements' => [
        new Sematico\Metapress\Requirements\Checks\PHPVersion('7.4'),
        new Sematico\Metapress\Requirements\Checks\WPVersion('5.6'),
    ],
];

The example above sets the minimum PHP version to 7.4 and the minimum WordPress version to 5.6. You can add as many requirements as needed to the array.

Available checks

The following checks are available to set the requirements of your plugin:

  • Callback: Allows you to define a custom callback to check a requirement.
  • ClassExists: Determines if one or more classes exist.
  • ExtensionLoaded: Determines if one or more PHP extensions are loaded.
  • PluginActive: Determines if one plugin is active.
  • PHPVersion: Sets the minimum PHP version required by the plugin.
  • WPVersion: Sets the minimum WordPress version required by the plugin.

Callback check

The Callback check allows you to define a custom callback to check a requirement. The callback should return a boolean value of true if the requirement is met, or an instance of WP_Error if the requirement is not met.

php
return [
    'requirements' => [
        new Sematico\Metapress\Requirements\Checks\Callback(
            function( $params ){
                return true;
            },
            [
                'params' => 'value',
            ]
        ),
    ],
];

ClassExists check

The ClassExists check determines if one or more classes exist. The check accepts an array of class names to check.

php
return [
    'requirements' => [
        new Sematico\Metapress\Requirements\Checks\ClassExists([
            'Sematico\Metapress\Requirements\Checks\PHPVersion',
            'Sematico\Metapress\Requirements\Checks\WPVersion',
        ]),
    ],
];

ExtensionLoaded check

The ExtensionLoaded check determines if one or more PHP extensions are loaded. The check accepts an array of extension names to check.

php
return [
    'requirements' => [
        new Sematico\Metapress\Requirements\Checks\ExtensionLoaded([
            'mbstring',
        ]),
    ],
];

PluginActive check

The PluginActive check determines if one plugin is active. The check accepts the plugin's slug to check.

php
return [
    'requirements' => [
        new Sematico\Metapress\Requirements\Checks\PluginActive('akismet'),
    ],
];

PHPVersion check

The PHPVersion check sets the minimum PHP version required by the plugin. The check accepts the minimum PHP version as a string.

php
return [
    'requirements' => [
        new Sematico\Metapress\Requirements\Checks\PHPVersion('7.4'),
    ],
];

WPVersion check

The WPVersion check sets the minimum WordPress version required by the plugin. The check accepts the minimum WordPress version as a string.

php
return [
    'requirements' => [
        new Sematico\Metapress\Requirements\Checks\WPVersion('5.6'),
    ],
];

Creating custom checks

You can create custom checks by extending the Sematico\Metapress\Requirements\Checks\AbstractCheck class and must implement the CheckInterface interface. The class must implement the check method that returns a boolean value of true if the requirement is met, or an instance of WP_Error if the requirement is not met.

php
namespace Sematico\Metapress\Requirements\Checks;

use Sematico\Metapress\Requirements\Checks\Interfaces\CheckInterface;
use WP_Error;

class CustomCheck extends AbstractCheck implements CheckInterface {

    public function check(): bool|WP_Error {
        // Check the requirement here.
    }

}

You can then use the custom check in the plugin's configuration file.

php
return [
    'requirements' => [
        new Sematico\Metapress\Requirements\Checks\CustomCheck(),
    ],
];

Reference documentation