Skip to main content

Lifecycle hooks

In some cases, it can be very benificial to run some PHP in your tenants when certain important events happen. It could be important to add some user meta to the newly created user or update some posts when the tenant has been created. To enable this, WPCS supports several WordPress hooks that are called during important lifecycle events.

To use these hooks, you can use a PHP snippet plugin or create your own custom plugin that implements these hooks. The choice is yours.

Tenant hooks

Tenant created

After tenant creation, the hook wpcs_tenant_created is fired. The only argument is the External ID.

Example
add_action('wpcs_tenant_created', 'after_tenant_created');

function after_tenant_created($external_id) {
// Your code to run after the tenant is created.
}

Pre tenant deletion

Before the tenant is deleted, the hook wpcs_tenant_pre_delete is fired. Its only arguments is the External ID.

This hook can for example be used to clean up external systems, or send a signal to your CRM that deletion has been performed.

Example
add_action('wpcs_tenant_pre_delete', 'before_tenant_deletion');

function before_tenant_deletion($external_id) {
// Your code to run BEFORE the tenant is deleted.
}

Pre main domain change

Before the tenant's main domain is changed, the hook wpcs_tenant_pre_main_domain_change is fired. Its arguments are:

  1. The tenant's External ID
  2. The old domain name
  3. The new domain name
Example
add_action('wpcs_tenant_pre_main_domain_change', 'before_tenant_domain_change', 10, 3);

function before_tenant_domain_change($external_id, $old_domain, $new_domain) {
// Your code to run BEFORE the tenant's domain is changed.
}

Tenant moved

After you have moved a tenant to a different version, the hook wpcs_tenant_moved is fired. Its arguments are:

  1. The tenant's External ID
  2. The old version ID
  3. The new version ID
Example
add_action('wpcs_tenant_moved', 'after_tenant_moved', 10, 3);

function after_tenant_moved($external_id, $old_version_id, $new_version_id) {
// Your code
}

Post main domain change

Before the tenant's main domain is changed, the hook wpcs_tenant_main_domain_changed is fired. Its arguments are:

  1. The tenant's External ID
  2. The old domain name
  3. The new domain name
Example
add_action('wpcs_tenant_main_domain_changed', 'tenant_domain_changed', 10, 3);

function tenant_domain_changed($external_id, $old_domain, $new_domain) {
// Your code to run AFTER the tenant's domain is changed to $new_domain.
}

Tenant user hooks

Pre tenant user created

Before the tenant user is created, the hook wpcs_tenant_user_pre_create is fired. Its arguments are:

  • The tenant's External ID
  • The WP user's username
  • The WP user's E-mail address
  • The WP user's role

Only after this hook is fired, the user created. This hook should be used to prepare the environment for a new user if it needs to be prepared.

Example
add_action('wpcs_tenant_user_pre_create', 'before_tenant_user_creation', 10, 4); // Be sure to specify the argument count!

function before_tenant_user_creation($external_id, $username, $email, $role) {
// Your code to run BEFORE the tenant's WP user is created.
}
caution

This hook cannot be used to add user metadata, as the user does not exist at this point. If you need to add metadata or perform actions on the user itself, use the wpcs_tenant_user_created hook.

Tenant user created

After the tenant user is created, the hook wpcs_tenant_user_created is fired. Its arguments are:

  • The tenant's External ID
  • The newly created WP user's ID
Example
add_action('wpcs_tenant_user_created', 'after_tenant_user_created', 10, 2); // Be sure to specify the argument count!

function after_tenant_user_created($external_id, $user_id) {
// Your code to run after the tenant's WP user is created.
// You can look up the user via the ID
}