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 or 1.1
}

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', '>=')
) {
    // Running October CMS 2.2 or above
}
else {
    // Running a legacy version
}

Another example:

if (!class_exists('System')) {
    // Running October CMS 1.0 or 1.1
}
// 2.0 only
elseif (version_compare(\System::VERSION, '2.0', '=')) {
    // Running October CMS 2.0 only (not including 2.1, 2.2, etc.)
}

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

5 Likes

On a system running v3.3, here is the expected output:

> version_compare(\System::VERSION, '3.4')
= -1

> version_compare(\System::VERSION, '3.3')
= 0

> version_compare(\System::VERSION, '3.2')
= 1

Semantically, I believe it works best using the third operator, like this:

if (version_compare(\System::VERSION, '3.4', '>=')) {
    // Running October CMS 3.4 or later
}
else {
    // Or not...
}
2 Likes