WordPress Hook Directory: activate_blog

Spread the love

WordPress Hook Directory: activate_blog

The activate_blog action hook in WordPress is triggered when a site in a multisite network is activated. This hook is particularly useful for developers who want to execute custom code when a new site is activated, such as setting up default options, configuring site-specific settings, or adding custom database entries.


Hook Name

activate_blog


Hook Type

Action Hook


Parameters

The activate_blog hook accepts the following parameter:

  1. $blog_id (int): The ID of the site being activated.

This single parameter allows you to perform actions specific to the activated site.


Use Case Scenarios

  • Automatically creating default pages (e.g., “About Us” or “Contact Us”) for a new site.
  • Setting default options in the database for the newly activated site.
  • Sending an email notification to the site administrator upon activation.
  • Configuring custom post types or taxonomies for the activated site.

Basic Example

Here’s a basic example of how to use the activate_blog hook to log a message when a new site is activated:

add_action('activate_blog', 'custom_activate_blog_action');

function custom_activate_blog_action($blog_id) {
    // Log the blog activation
    error_log('Site activated: ' . $blog_id);
}

This code adds a custom action to the activate_blog hook, logging the ID of the newly activated site.


Practical Example: Creating Default Pages

The following example demonstrates how to automatically create default pages (like “About Us” and “Contact Us”) when a new site is activated:

add_action('activate_blog', 'create_default_pages_for_new_site');

function create_default_pages_for_new_site($blog_id) {
    // Switch to the new blog
    switch_to_blog($blog_id);

    // Create the "About Us" page
    wp_insert_post([
        'post_title'   => 'About Us',
        'post_content' => 'This is the About Us page.',
        'post_status'  => 'publish',
        'post_type'    => 'page',
    ]);

    // Create the "Contact Us" page
    wp_insert_post([
        'post_title'   => 'Contact Us',
        'post_content' => 'This is the Contact Us page.',
        'post_status'  => 'publish',
        'post_type'    => 'page',
    ]);

    // Restore to the current blog
    restore_current_blog();
}

In this example:

  1. switch_to_blog() is used to switch the context to the newly activated site.
  2. wp_insert_post() is used to create new pages for the site.
  3. restore_current_blog() ensures that the context is returned to the original site after creating the pages.

Advanced Example: Setting Default Options

Another practical use of the activate_blog hook is setting up default options in the database for a newly activated site:

add_action('activate_blog', 'set_default_site_options');

function set_default_site_options($blog_id) {
    // Switch to the new blog
    switch_to_blog($blog_id);

    // Set default options
    update_option('default_theme_color', 'blue');
    update_option('default_layout', 'grid');

    // Restore to the current blog
    restore_current_blog();
}

This code automatically sets up default options like default_theme_color and default_layout for the new site.


WordPress Version History for activate_blog

The activate_blog hook was introduced in WordPress 3.0, when multisite functionality became a core feature. Since its introduction, it has remained stable and is widely used in multisite network development.


Key Notes

  1. Multisite-Specific Hook: The activate_blog hook is only applicable in WordPress multisite installations. It will not work in single-site setups.
  2. Switching Context: Always use switch_to_blog() and restore_current_blog() when performing actions specific to a particular site.
  3. Testing: Thoroughly test your code in a staging environment to ensure it doesn’t interfere with other site activations on the network.
  4. Avoid Conflicts: Use unique function names or prefixes to prevent naming collisions with other plugins or themes.

Full Tutorial: How to Use the activate_blog Hook

Step 1: Enable Multisite

Ensure that WordPress Multisite is enabled in your installation. If not, follow the official guide to enable Multisite.

Step 2: Add the Hook to Your Plugin or Theme

Include the activate_blog hook in your plugin or theme’s functions.php file. For example:

add_action('activate_blog', 'my_custom_activate_blog');

function my_custom_activate_blog($blog_id) {
    // Your custom code here
}

Step 3: Perform Actions

Decide what actions you want to perform upon site activation, such as:

  • Creating default pages.
  • Setting default options.
  • Sending notifications.

Step 4: Test Your Code

Activate a new site in your multisite network to ensure that the custom actions are executed correctly.


Example Image

Below is an image demonstrating how the activate_blog hook is triggered and used in WordPress:

 


By using the activate_blog hook effectively, you can automate essential tasks for newly activated sites in a multisite network, saving time and ensuring consistency across your network.

Related Posts

Leave a Reply