Call function from plugin in template In craft 2 I had a simple plugin that I used to run some php code. I could call the function like this.

{{ craft.pluginname.functionname }}


However I have been unable to do the same thing in craft 3. I have created a new plugin for craft 3. Moved my functions to the main plugin file in src.

Is there a new way to call a function from a plugin in craft 3?

Ok got it figured out.

author below = root plugin path in vendor folder

In main plugin file had to add

use author\pluginname\variables\variablename;
use craft\web\twig\variables\CraftVariable;


Updated init event to

Event::on(
    CraftVariable::class,
    CraftVariable::EVENT_INIT,
    function (Event $event) {
        /** @var CraftVariable $variable */
        $variable = $event-&gt;sender;
        $variable-&gt;set('pluginname', variablename::class);
    }
);


Created variables file in variables folder src/variables/variablename.php

namespace author\pluginname\variables;
use author\pluginname\mainClassName;
use Craft;

class variablename
{
    //Custom Functions Here
    public function test() {
        echo 'Test';
    }
}


Then from a template I was able to use

{{ craft.pluginname.test }}
You'll need to register your variable in your plugin's init() method, like this:

Event::on(
    CraftVariable::class,
    CraftVariable::EVENT_INIT,
    function (Event $event) {
        /** @var CraftVariable $variable */
        $variable = $event-&gt;sender;
        $variable-&gt;set('pluginname', PluginVariable::class);
    }
);


c.f.: https://docs.craftcms.com/v3/extend/extending-twig.html