Get the plugin version from the plugin PHP code

Get the plugin version from the plugin PHP code

by Thomas Chapeaux -
Number of replies: 4

Hello,

I'm trying to access my plugin version, defined in `version.php`, from elsewhere in my plugin PHP code.

The use case is: my plugin makes several calls to an external server, and I'd like to add the plugin version to each request so that it can be logged in the external server.

Is there any way to get the currently installed plugin version without hardcoding anything?

Thanks in advance for your answers!

Average of ratings: -
In reply to Thomas Chapeaux

Re: Get the plugin version from the plugin PHP code

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

I'm honestly not sure if its in a variable that you can call (looking forward to learning that!)

But worst case scenario, if it isn't, then it is held in the database, so you could make a database call and store it as a variable in your plugin to use and send to the external server. Should be something like:

SELECT value FROM {config_plugins} WHERE plugin = 'your_pluginname' and name = 'version';

In reply to Richard Oelmann

Re: Get the plugin version from the plugin PHP code

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
get_config('mod_mypluginname')->version;
Should get it
Average of ratings: Useful (2)
In reply to Thomas Chapeaux

Re: Get the plugin version from the plugin PHP code

by Andreas Grabs -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Translators

Hi Thomas,

to get the version of any plugin even subplugin you could write a simple function like that:

function get_version_from_plugin($component) {
    list($plugintype, $pluginname) = core_component::normalize_component($component);
    $pluginpath = core_component::get_plugin_directory($plugintype, $pluginname);

    $plugin = new \stdClass();
    require $pluginpath.'/version.php';

    return $plugin->version;
}

Then you can use this function like that:

$qtypeversion = get_version_from_plugin('qtype_calculated');

I hope this helps.

Best regards
Andreas

In reply to Thomas Chapeaux

Re: Get the plugin version from the plugin PHP code

by Monika Gujar -
Hello,
Try using below code...

$version = get_config('mod_xyz')->version;

where 'mod' is your plugin type and 'xyz' is your plugin name