Answers.Body: <p>There's an event you can listen to:</p>
  
  <pre class="lang-php prettyprint-override"><code>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-&gt;plugin === $this) {
                      // Do stuff here, we were just installed
                  }
              }
          );
  </code></pre>
  
  <p>If you generate your plugin scaffolding via <a href="https://pluginfactory.io/" rel="nofollow noreferrer">pluginfactory.io</a> it will generate the above event for you in your base plugin.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-13T13:56:16.943Z
Answers.LastActivityDate: 2019-06-13T13:56:16.943Z
Answers.UserDisplayName: andrew.welch
Body: <p>How can I run a function when my plugin is installed? I tried SafeUp in migrations but it doesn't work.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-06-13T13:23:42.003Z
Comments.Text: You tried making an [Install migration](https://docs.craftcms.com/v3/extend/migrations.html#plugin-install-migrations)?
Comments.UserDisplayName: Oli
Tags: <plugin-development><craft3>
Title: How to run function after plugin installed?
ThreadText: # How to run function after plugin installed?
  
  (Asked by: username_0 on 2019-06-13)
  
  How can I run a function when my plugin is installed? I tried SafeUp in migrations but it doesn't work.
  
  **Tags:** plugin-development, craft3
  
  > **Comments:**
  > **username_1**: You tried making an [Install migration](https://docs.craftcms.com/v3/extend/migrations.html#plugin-install-migrations)?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-06-13. Score: 3
  
  There's an event you can listen to:
  
  ```php
  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](https://pluginfactory.io/) it will generate the above event for you in your base plugin.
  
  
  ---
  
  