3

Is it possible to auto enable/install a plugin through config (or another way) rather than having to do this through the database?

3

The short answer is no. The long answer is sortof, if you’re running Craft 3.

Since plugins are technically modules in Craft 3, you should be able to bootstrap a plugin onto the application just like any other module.

To do that, update config/app.php with these modules and bootstrap sub-arrays:

return [
    'modules' => [
        'contact-form' => function() {
            $plugin = Craft::$app->plugins->createPlugin('contact-form');
            $plugin->setSettings([
                'toEmail' => 'foo@bar.baz',
            ]);
            return $plugin;
        }
    ],

    'bootstrap' => [
        'contact-form',
    ],
];

Using Contact Form as an example. For a different plugin, replace all of the 'contact-form' strings with the right plugin handle, and change the settings array to whatever the plugin is expecting. (You can find which settings keys the plugin supports by checking its Settings model – usually either src/Settings.php or src/models/Settings.php.)

Some caveats off the top of my head, though:

  • Craft won’t know that the plugin is installed, so it will still look like an uninstalled plugin in Settings → Plugins.
  • You won’t be able to edit the plugin’s settings in the Control Panel; only from config/app.php.
  • You won’t be able to update the plugin from the Control Panel; only via composer update.
  • If the plugin has an install migration, you will need to manually duplicate it as content migrations. And whenever you update the plugin, you’ll need to check if it added any new migrations, and manually duplicate those as content migrations as well. (Any migrations that were there at the time you first installed the plugin, besides the Install migration, can be ignored.)

EDIT

If you just want to be able to install a plugin programmatically, but don’t mind if the plugin ends up getting a row in the plugins DB table, that’s easy. Just create a new content migration, with the following safeUp() function:

public function safeUp()
{
    Craft::$app->plugins->installPlugin('plugin-handle');
}

Then you just need to run the migration on each of your environments. You can either do that from Utilities → Migrations in the Control Panel, or by running the following CLI command:

./craft migrate/up

Maybe worth noting that you can also install plugins directly from the terminal, using this command:

./craft install/plugin <plugin-handle>
| improve this answer | |
  • Thanks for taking the time to answer Brandon - sounds it is possible like you say. I am just wondering if what I am trying to do would be possible with a migration just to add the record to the plugins table? There's probably a reason you haven't suggested this but that seems to be the only difference between an installed and uninstalled plugin? – Mike F Jan 22 '18 at 9:20
  • @MikeF Sorry, I must have really misunderstood your question! Sounds like you just meant you want to install plugins programatically? Just added a second part to my answer for that. – Brandon Kelly Jan 23 '18 at 3:35
  • Brandon - that's exactly what I was looking for! Thank you so much ❤️ – Mike F Jan 26 '18 at 11:34
  • content migration link is old, the new one: docs.craftcms.com/v3/extend/migrations.html#creating-migrations – Visky Máté Jun 5 at 2:12

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.