Using WordPress ‘make_spam_blog’ PHP action

The make_spam_blog WordPress PHP action fires when the ‘spam’ status is added to a site.

Usage

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

function your_custom_function($site_id) {
    // your custom code here

    return $site_id;
}

Parameters

  • $site_id (int): Site ID.

More information

See WordPress Developer Resources: make_spam_blog

Examples

Send an email notification when a site is marked as spam

This example sends an email notification to the administrator when a site is marked as spam.

add_action('make_spam_blog', 'send_spam_notification', 10, 1);

function send_spam_notification($site_id) {
    $admin_email = get_option('admin_email');
    $subject = 'Site marked as spam';
    $message = "Site ID $site_id has been marked as spam.";
    wp_mail($admin_email, $subject, $message);
}

Log spam sites to a custom log file

This example logs the Site ID of a spam site to a custom log file.

add_action('make_spam_blog', 'log_spam_site', 10, 1);

function log_spam_site($site_id) {
    $log_file = '/path/to/spam_sites.log';
    $log_entry = "Site ID $site_id marked as spam on " . date('Y-m-d H:i:s') . "\n";
    file_put_contents($log_file, $log_entry, FILE_APPEND);
}

Block a user associated with a spam site

This example blocks the user associated with a spam site by updating the user’s meta information.

add_action('make_spam_blog', 'block_user_from_spam_site', 10, 1);

function block_user_from_spam_site($site_id) {
    $user_id = get_blog_option($site_id, 'user_id');
    update_user_meta($user_id, 'blocked', 1);
}

Add a custom prefix to the spam site’s title

This example adds a custom prefix to the title of a spam site.

add_action('make_spam_blog', 'add_spam_prefix_to_title', 10, 1);

function add_spam_prefix_to_title($site_id) {
    $current_title = get_blog_option($site_id, 'blogname');
    $new_title = "[SPAM] " . $current_title;
    update_blog_option($site_id, 'blogname', $new_title);
}

Remove all posts from a spam site

This example removes all posts from a site marked as spam.

add_action('make_spam_blog', 'delete_posts_from_spam_site', 10, 1);

function delete_posts_from_spam_site($site_id) {
    switch_to_blog($site_id);
    $posts = get_posts(array('numberposts' => -1));
    foreach ($posts as $post) {
        wp_delete_post($post->ID, true);
    }
    restore_current_blog();
}