2

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

2

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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