Using WordPress ‘archive_blog’ PHP action

The archive_blog WordPress action fires when the ‘archived’ status is added to a site.

Usage

add_action('archive_blog', function($site_id) {
    // your custom code here
});

Parameters

  • $site_id (int): The site ID.

More information

See WordPress Developer Resources: archive_blog

Examples

Send an email when a blog is archived

Send an email to the site administrator when a blog is archived.

add_action('archive_blog', function($site_id) {
    $admin_email = get_option('admin_email');
    $subject = "Blog Archived";
    $message = "Blog with ID: {$site_id} has been archived.";
    wp_mail($admin_email, $subject, $message);
});

Log blog archival

Log when a blog is archived in a custom log file.

add_action('archive_blog', function($site_id) {
    $log_message = "Blog with ID: {$site_id} was archived on " . date("Y-m-d H:i:s") . PHP_EOL;
    file_put_contents('archived_blogs.log', $log_message, FILE_APPEND);
});

Remove blog archive date from cache

Remove the blog archive date from the cache when a blog is archived.

add_action('archive_blog', function($site_id) {
    $cache_key = "archive_date_{$site_id}";
    wp_cache_delete($cache_key);
});

Add an entry to a custom table when a blog is archived

Add a row to a custom database table when a blog is archived.

add_action('archive_blog', function($site_id) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'archived_blogs';
    $data = array('site_id' => $site_id, 'archived_at' => current_time('mysql'));
    $wpdb->insert($table_name, $data);
});

Update custom post type status when a blog is archived

Update the status of custom post type ‘my_custom_post_type’ to ‘draft’ when a blog is archived.

add_action('archive_blog', function($site_id) {
    global $wpdb;
    $wpdb->update(
        $wpdb->posts,
        array('post_status' => 'draft'),
        array('post_type' => 'my_custom_post_type', 'blog_id' => $site_id)
    );
});