3

How can I run a function when my plugin is installed? I tried SafeUp in migrations but it doesn't work.

1

1 Answer 1

3

There's an event you can listen to:

use craft\events\PluginEvent;
use craft\services\Plugins;

        // Handler: EVENT_AFTER_INSTALL_PLUGIN
        Event::on(
            Plugins::class,
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
            function (PluginEvent $event) {
                if ($event->plugin === $this) {
                    // Do stuff here, we were just installed
                }
            }
        );

If you generate your plugin scaffolding via pluginfactory.io it will generate the above event for you in your base plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.