Hacked WordPress spam in Google: get the spam pages out, then lock WordPress down

When a WordPress site has been hacked with spam pages, cleaning the site is only half the job: Google keeps listing the spam addresses until it recrawls each one and sees it is gone. howitwork.net had 265 spam URLs still indexed after its clean-up. This guide shows the order that works: clean the site, make every spam address answer 404 or 410, help Google recrawl them, then lock WordPress down. All the code is from the live site.

Is your site affected?

This hack is often called a Japanese keyword hack or a casino spam hack. Look for these signs:

  • A Google search for site:yourdomain.com shows pages you never wrote: casino, betting, pills, replica bags, or titles in Japanese or Chinese.
  • In Search Console, Indexing > Pages shows far more indexed pages than your site has, or Security issues reports hacked content.
  • Search Console sends a “new owner” message you did not expect. Attackers sometimes verify themselves as owners so they can submit their own sitemaps. Remove any owner you don’t recognise under Settings > Users and permissions.
  • A spam address opens your normal page or a “not found” page for you, but Google shows spam text for it. Some hacks show the spam only to Googlebot. Use URL inspection > Test live URL > View tested page to see what Google receives.

1. Clean the site first

Removing spam from Google is the last step. If the attacker still has a way in, the spam comes back.

  • Change every password: WordPress administrators, the hosting panel, FTP/SFTP and the database.
  • Delete any user you don’t recognise, especially administrators.
  • Update WordPress, every plugin and every theme; delete the ones you don’t use.
  • Scan with a security plugin (for example Wordfence) or your host’s scanner, and look for .php files in wp-content/uploads, which should only hold media and documents.
  • Check the places this hack usually hides: unknown files in wp-content/mu-plugins, plugins you never installed, changed .htaccess or wp-config.php files, new administrator accounts and unfamiliar scheduled tasks (a plugin such as WP Crontrol lists them).
  • If spam pages keep coming back after you delete them, something is still creating them. Don’t move on to the Google steps until a new spam address stops appearing.
  • Keep a backup before you change code, and test on a staging copy if you have one.

2. How a spam page leaves Google

Google has to visit the old address again. When the server answers 404 Not Found or 410 Gone, Google’s documentation says it treats the page as not existing and drops it from the index. 410 is the honest answer for pages removed on purpose, so that is what this site uses.

  • Don’t redirect spam URLs to the home page. Google treats a redirect to an unrelated page as a soft 404, which is slower to clear.
  • Don’t show an empty page with 200 OK. That tells Google the page still exists.
  • Don’t block the spam URLs in robots.txt. If Google can’t crawl them, it can’t see the 404 or 410.

3. A must-use plugin that answers 410

Save it as wp-content/mu-plugins/howitwork-410-gone.php (create the folder if needed). Must-use plugins load automatically and can’t be switched off in the Plugins screen. Real pages are never touched: it only acts when WordPress has already decided the page doesn’t exist.

add_action( 'template_redirect', function () {
    if ( ! is_404() ) {
        return; // real page: do nothing
    }
    // Never touch sitemaps (howitwork-site.php turns those into 200 at priority 1).
    if ( get_query_var( 'sitemap' ) !== '' || get_query_var( 'sitemap-stylesheet' ) !== '' ) {
        return;
    }

    status_header( 410 );
    nocache_headers();
    header( 'X-Robots-Tag: noindex', true );

The rest of the function prints a short “This page no longer exists” page with links to the home page and the tools, then exits. It runs at priority 20 on template_redirect.

4. The bug in the first version: check the hook order

This site has another small plugin that makes the WordPress sitemap always answer 200, at priority 1:

add_action('template_redirect', static function (): void {
    if (get_query_var('sitemap') === '' && get_query_var('sitemap-stylesheet') === '') {
        return;
    }
    global $wp_query;
    $wp_query->is_404 = false;
    status_header(200);
}, 1);

The first version of the 410 plugin, written with an AI assistant, ran at priority 0, before that fix, so a sitemap request WordPress had marked as not found would have been answered 410 Gone. The AI had written it before reading the rest of the site’s code. About half an hour later, once it had read the other plugin, it made the fix (version 1.1): skip sitemap requests and run at priority 20:

    if ( ! is_404() ) {
        return; // real page: do nothing
    }
+   // Never touch sitemaps (howitwork-site.php turns those into 200 at priority 1).
+   if ( get_query_var( 'sitemap' ) !== '' || get_query_var( 'sitemap-stylesheet' ) !== '' ) {
+       return;
+   }
 ...
-}, 0 );
+}, 20 );

Lesson for AI-written code: give the assistant everything that runs on the same hook, ask “what else could this break?” (sitemaps, feeds, login, admin), and check the status codes of the pages that matter after every deploy.

5. Send empty spam archives to the 410

Category, tag and date archives created by the spam posts still open, empty. This marks them as not found so the 410 plugin (priority 20) answers for them:

add_action( 'template_redirect', function () {
    if ( ! ( is_category() || is_tag() || is_date() ) || have_posts() ) {
        return;
    }
    global $wp_query;
    $wp_query->set_404();
    status_header( 404 );
}, 10 );

Get the list of spam addresses

You need the exact addresses for the sitemap and to check your work. Search Console has them:

  1. Open Indexing > Pages. Click View data about indexed pages to see what is still indexed, or click a reason row such as “Crawled – currently not indexed” to see spam Google found but has not listed.
  2. Click Export (top right) and choose CSV. The download is a ZIP; the addresses are in Table.csv.
  3. Keep only the spam. Your real pages are mixed in, so sort by address and remove anything you recognise.
  4. Each export stops at 1,000 rows. For a bigger hack, export each reason separately and look for a shared pattern, such as a word every spam address contains.

6. Help Google recrawl: a temporary sitemap of removed URLs

A sitemap that lists only the removed addresses, each with the removal date as lastmod, tells Google they changed since its last visit. It is served at /removed-urls.xml:

add_action( 'init', function () {
    $path = wp_parse_url( $_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH );
    if ( $path !== '/removed-urls.xml' ) {
        return;
    }
    status_header( 200 );
    header( 'Content-Type: application/xml; charset=utf-8' );
    header( 'X-Robots-Tag: noindex', true );
    $home = untrailingslashit( home_url() );
    echo '<?xml version="1.0" encoding="UTF-8"?>' . "n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "n";
    foreach ( preg_split( '/R/', trim( HOWITWORK_REMOVED_URLS ) ) as $p ) {
        echo '<url><loc>' . esc_url( $home . $p ) . '</loc><lastmod>' . HOWITWORK_REMOVED_ON . '</lastmod></url>' . "n";
    }
    echo '</urlset>';

HOWITWORK_REMOVED_URLS is the list exported from Search Console (one path per line), and HOWITWORK_REMOVED_ON the date the pages were removed. Check that every URL on the list really answers 410 before you submit it.

7. In Google Search Console

  1. Under Sitemaps, submit https://your-site/removed-urls.xml. Remove it (and the list) again once the pages have dropped out. That often takes weeks and can take months.
  2. For the worst pages, use the Removals tool. It hides a URL from results quickly, but only for about six months; the 410 does the permanent work.
  3. Keep your normal sitemap (/wp-sitemap.xml) submitted and answering 200.

Prefer not to write code? I turned these steps into a free plugin, After the Hack. It imports the list from Search Console, answers the spam with 410, publishes the temporary sitemap and tracks the recovery.

8. Lock WordPress down

add_filter('xmlrpc_enabled', '__return_false');
add_filter('wp_is_application_passwords_available', '__return_false');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_generator');
add_filter('the_generator', '__return_empty_string');

add_filter('rest_endpoints', static function (array $endpoints): array {
    unset($endpoints['/wp/v2/users'], $endpoints['/wp/v2/users/(?P<id>[d]+)']);
    return $endpoints;
});
  • XML-RPC off: an old remote-publishing interface that attackers use to try many passwords in one request.
  • Application passwords off if no app needs them.
  • No version number or discovery links in the page head.
  • No public users list in the REST API. The site also redirects author archives to the home page, so usernames aren’t handed out.
  • One login error for everything: add_filter('login_errors', static fn(): string => 'Sign-in failed.'); WordPress’s default message says when a username exists.

Slow down password guessing

After 8 failed sign-ins from one visitor (stored as a hash of the address), sign-in is refused for 15 minutes. A wp_login_failed action adds one to the counter; a successful login clears it.

add_filter('authenticate', static function ($user, $username, $password) {
    if (!is_string($username) || $username === '' || $password === '') {
        return $user;
    }
    $key = 'hiw_login_' . howitwork_ip_hash();
    if ((int) get_transient($key) >= 8) {
        return new WP_Error('too_many', 'Too many sign-in attempts. Wait 15 minutes.');
    }
    return $user;
}, 30, 3);

No code editing from the dashboard

if (!defined('DISALLOW_FILE_EDIT')) {
    define('DISALLOW_FILE_EDIT', true);
}

This removes the built-in theme and plugin file editor, so a stolen admin password can’t be used to hide code in your theme in seconds. It doesn’t stop everything an administrator can do: strong, unique passwords and two-factor sign-in still matter. It can also go in wp-config.php.

How long it took on this site

From the Search Console Pages report for howitwork.net, with the 410 answers and the temporary sitemap in place from 1 November 2026:

  • 1 November: 275 pages indexed, only about 14 of them real. All 265 spam addresses answered 410.
  • 6 November: 181 indexed.
  • 9 November: 162 indexed, while the number of real pages in the index went up.

About 110 spam listings dropped out in the first eight days. The rest take longer, because Google revisits low-value addresses less often. A site Google crawls more often will usually clear faster.

Common questions

Should I just delete the whole site and start again?

Usually not. The spam addresses stay in Google whatever you do with the site. They only drop out when each one answers 404 or 410. A fresh install helps only if you can’t find how the attacker got in, and you still need the steps in this guide afterwards.

404 or 410: does it really matter?

Both remove the page. 410 says it was removed on purpose, which is the honest answer here, and in practice it can clear a little sooner. A plain 404 is fine if your setup makes 410 hard.

Will the spam hurt my real pages in Google?

While it is listed, it shows up under your name and takes up Google’s visits to your site, so real pages get crawled less often. Once the spam answers 410, that recovers.

Do I need to keep the 410 code forever?

Keep it until the Pages report no longer shows the spam as indexed, then a few weeks more. After that, removing it only changes the answer from 410 to your normal 404, which is still fine.

Checklist

  1. Clean the site: passwords, users, updates, scan.
  2. Every spam URL answers 404 or 410; no redirects home, no robots.txt block.
  3. Temporary sitemap of the removed URLs, submitted in Search Console; Removals tool for the worst ones.
  4. Lock-down: XML-RPC, users list, one login error, login limit, no file editor.
  5. After any change, check the status codes of your sitemap, home page and a spam URL.

More IT guides · howITwork on YouTube