Checking the October CMS version in PHP code

With the recent release of October CMS v3.0, this advice should help with managing plugins across multiple versions. To use different logic depending on the October CMS version. Here is a way for you to check the exact version.

A simple way to check if October CMS is running v2 or above:

if (class_exists('System'))  {
    // Running October CMS 2.0 or above
}
else {
    // Running October CMS 1.0
}

For more precise checking, you can include an extra step with version_compare. This next example checks if it is running v2.2 or above:

if (
    class_exists('System') &&
    version_compare(\System::VERSION, '2.2') !== -1
) {
    // Running October CMS v2.2 or above
}
else {
    // Running a legacy version
}

Another example:

// 1.0
if (!class_exists('System')) {
    // ...
}
// 2.0
elseif (version_compare(\System::VERSION, '3.0') === -1) {
    // ...
}

This is the strategy we use internally with the RainLab plugins. I hope you find this useful as well.

4 Likes