Using WordPress ‘activate_blog’ PHP action

The ‘activate_blog’ WordPress PHP action triggers when a network site is activated.

Usage

To use this function, simply add the following code to your theme’s functions.php file or a custom plugin:

add_action('activate_blog', 'your_custom_function', 10, 1);

function your_custom_function($id) {
    // Your custom code goes here
}

Parameters

  • $id (int): The ID of the activated site.

Examples

Send a welcome email to the site administrator

add_action('activate_blog', 'send_welcome_email', 10, 1);

function send_welcome_email($id) {
    $admin_email = get_blog_option($id, 'admin_email');
    $subject = 'Welcome to the network!';
    $message = 'Congratulations, your site has been activated!';

    wp_mail($admin_email, $subject, $message);
}

In this example, a welcome email is sent to the site administrator when a new network site is activated.

Log activation in a custom table

add_action('activate_blog', 'log_activation', 10, 1);

function log_activation($id) {
    global $wpdb;

    $table_name = $wpdb->prefix . 'activations';
    $current_time = current_time('mysql');

    $wpdb->insert($table_name, array('site_id' => $id, 'activation_time' => $current_time));
}

This example logs the activation of a network site in a custom table with the site ID and activation time.

Create a default post on activation

add_action('activate_blog', 'create_default_post', 10, 1);

function create_default_post($id) {
    switch_to_blog($id);

    $post_data = array(
        'post_title' => 'Welcome to our network!',
        'post_content' => 'This is your first post on your new site.',
        'post_status' => 'publish',
    );

    wp_insert_post($post_data);

    restore_current_blog();
}

This example creates a default post with a welcome message when a new network site is activated.

add_action('activate_blog', 'update_permalink_structure', 10, 1);

function update_permalink_structure($id) {
    switch_to_blog($id);

    $permalink_structure = '/%postname%/';
    update_option('permalink_structure', $permalink_structure);

    restore_current_blog();
}

In this example, the permalink structure of the activated site is updated to a custom format.

Assign a default theme to the new site

add_action('activate_blog', 'assign_default_theme', 10, 1);

function assign_default_theme($id) {
    switch_to_blog($id);

    $theme = 'my-default-theme';
    switch_theme($theme);

    restore_current_blog();
}

This example assigns a default theme to the newly activated network site.