# Activation

The activate logic is some logic that execute when the plugin is activated.

This happens in two cases:

* when the plugin installs.
* when the plugin got disabled and is re-enabled.

It is possible to use that state to initialize the plugin:

* By creating tables.
* By creating initial configurations.

## Creating an activate logic

In Launchpad, to create a logic which will be executed when the plugin is activated, we need to use an Activator.

To create an activator, any class can be used as the only requirement is to [use the `@activate` annotation](https://github.com/wp-launchpad/launchpad-examples/blob/316e6927e24339550a81879fdaf189d73a9acf4b/activate/inc/MyActivator.php#L12) inside the docblock of at least one of the methods:

```php
class MyActivator {
    /**
    * @activate
    */
    public function register_options() {
        
    }
}
```

Once the activator class is created, it needs to be registered on a service provider to be loaded.

For that it is important to first [add the necessary logic into the provider](https://github.com/wp-launchpad/launchpad-examples/blob/316e6927e24339550a81879fdaf189d73a9acf4b/activate/inc/ServiceProvider.php#L9) using [`HasActivatorServiceProviderInterface`](https://github.com/wp-launchpad/core/blob/develop/inc/Activation/HasActivatorServiceProviderInterface.php) interface and [`HasActivatorServiceProviderTrait`](https://github.com/wp-launchpad/core/blob/develop/inc/Activation/HasActivatorServiceProviderTrait.php) trait:

```php
class Provider extends AbstractServiceProvider implements HasActivatorServiceProviderInterface {
   use HasActivatorServiceProviderTrait; 
   
   public function define() {

   }
} 
```

It is then possible to have access to the [`register_activator` method to register our activator](https://github.com/wp-launchpad/launchpad-examples/blob/316e6927e24339550a81879fdaf189d73a9acf4b/activate/inc/ServiceProvider.php#L18) inside [the `define` method](https://github.com/wp-launchpad/launchpad-examples/blob/316e6927e24339550a81879fdaf189d73a9acf4b/activate/inc/ServiceProvider.php#L17):

```php
class Provider extends AbstractServiceProvider implements HasActivatorServiceProviderInterface {
   use HasActivatorServiceProviderTrait; 
   
   public function define() {
    $this->register_activator(MyActivator::class);
   }
} 
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wp-launchpad.gitbook.io/launchpad/tutorials/index/index-1/activate.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
