WordPress Automation with Tenant Lifecycle Hooks

WordPress enables users to implement custom functionality through filters and actions. However, in certain scenarios, there's a need to respond to events outside the standard WordPress scope.

What are Tenant Lifecycle Hooks?

Tenant Lifecycle Hooks are specialized hooks that trigger when specific tenant-related events occur in a WordPress environment. These events include creating a new tenant, moving a tenant, or deleting a tenant. Such hooks allow for the execution of custom code in response to these events, thus providing a high level of control and customization for multi-tenant WordPress setups.

Common Tenant Lifecycle Events and Their Hooks

1. Tenant Creation: When a new tenant is created, the wpcs_tenant_created hook is fired. It takes the External ID as an argument.

add_action('wpcs_tenant_created', 'after_tenant_created');
function after_tenant_created($external_id) {
    // Custom code for post-creation actions
}

2. Pre Tenant Deletion: Before a tenant is deleted, the wpcs_tenant_pre_delete hook is triggered.

add_action('wpcs_tenant_pre_delete', 'before_tenant_deletion');
function before_tenant_deletion($external_id) {
    // Code for cleanup or external notifications
}

3. Tenant Movement: When a tenant is moved to a different version, wpcs_tenant_moved is invoked.

add_action('wpcs_tenant_moved', 'after_tenant_moved');
function after_tenant_moved($external_id, $old_version_id, $new_version_id) {
    // Code to handle tenant movement
}

4. Main Domain Changes: Hooks such as wpcs_tenant_pre_main_domain_change and wpcs_tenant_main_domain_changed are available for handling domain changes.

// Before domain change
add_action('wpcs_tenant_pre_main_domain_change', 'before_tenant_domain_change');
// After domain change
add_action('wpcs_tenant_main_domain_changed', 'tenant_domain_changed');

5. Tenant User Management: Hooks like wpcs_tenant_user_pre_create and wpcs_tenant_user_created help manage tenant user lifecycle events.

// Before user creation
add_action('wpcs_tenant_user_pre_create', 'before_tenant_user_creation');
// After user creation
add_action('wpcs_tenant_user_created', 'after_tenant_user_created');

Implementing Lifecycle Hooks in WordPress

To leverage these hooks, one must create a custom plugin. This plugin can be a must-use plugin, ensuring it's always active across your tenants. You can develop and install this plugin directly on the Version level from the wildcloud console. After deploying a Snapshot, your code changes, including plugins, themes, and language file alterations, are sent to your tenants.

Key Considerations

  • Direct File Access: It's important to note that direct file access is not possible from the wildcloud Console to maintain uniformity across applications. Use the CLI for creating must-use plugins containing Lifecycle Hooks.
  • Snapshots for Provisioning: Snapshots are crucial for provisioning new tenants and distributing code changes.