Using WordPress ‘make_spam_user’ PHP action

The make_spam_user WordPress PHP action fires after a user is marked as a SPAM user.

Usage

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

function your_custom_function($user_id) {
    // your custom code here
}

Parameters

  • $user_id (int): ID of the user marked as SPAM.

More information

See WordPress Developer Resources: make_spam_user

Examples

Log Spam User

Log the user ID of a marked SPAM user.

add_action('make_spam_user', 'log_spam_user', 10, 1);

function log_spam_user($user_id) {
    error_log("User marked as SPAM: " . $user_id);
}

Notify Admin

Send an email notification to the administrator when a user is marked as SPAM.

add_action('make_spam_user', 'notify_admin_spam_user', 10, 1);

function notify_admin_spam_user($user_id) {
    $admin_email = get_option('admin_email');
    $subject = "User marked as SPAM";
    $message = "User ID " . $user_id . " has been marked as SPAM.";
    wp_mail($admin_email, $subject, $message);
}

Remove User Posts

Delete all posts created by the user marked as SPAM.

add_action('make_spam_user', 'remove_spam_user_posts', 10, 1);

function remove_spam_user_posts($user_id) {
    $args = array(
        'author' => $user_id,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1
    );

    $user_posts = get_posts($args);

    foreach ($user_posts as $post) {
        wp_delete_post($post->ID, true);
    }
}

Add User Meta

Add a custom user meta to the user marked as SPAM.

add_action('make_spam_user', 'add_spam_user_meta', 10, 1);

function add_spam_user_meta($user_id) {
    update_user_meta($user_id, 'marked_as_spam', 'yes');
}

Block User Login

Prevent the user marked as SPAM from logging in.

add_action('make_spam_user', 'block_spam_user_login', 10, 1);

function block_spam_user_login($user_id) {
    $blocked = get_user_meta($user_id, 'blocked', true);

    if (!$blocked) {
        update_user_meta($user_id, 'blocked', 'yes');
    }
}