Skip to content

Hacked WordPress site: how to clean it step by step and prevent the next attack

· The AYT team · 4 min read

Man typing on a laptop at a wooden table, with a phone and notebook beside it

When a WordPress website is hacked, the order of the steps matters: first copy its current state and change every password, then scan, restore a clean backup or clean the files and database by hand, and finally close the hole the attacker used (usually an outdated plugin or theme, or a stolen password). Skip that last step and the site is hacked again within days.

Signs of a hacked WordPress site

  • visitors (especially on mobile or coming from Google) are sent to gambling, pharmacy or ad sites;
  • Google shows "This site may be hacked" in its results, or Chrome shows a red warning screen;
  • administrators you never created appear in wp-admin;
  • searching site:yourdomain.com in Google shows pages in Japanese, Chinese or with products you don't sell;
  • your host suspended the site, or the domain's emails land in spam because the server is sending spam.

If the main problem is a redirect to another site, the guide to WordPress spam redirects goes into detail on where the code hides.

Step 1: copy the current state

Saving an infected site sounds odd, but the copy later shows you what changed and how the attacker got in. Download the files and export the database. Don't restore it anywhere; keep it only for comparison.

If the site sends visitors to dangerous sites, put it in maintenance mode while you work.

Step 2: change every password

Change the passwords for every access point:

  • every administrator's password in wp-admin;
  • the hosting panel password and the FTP accounts;
  • the database user's password (then put it in wp-config.php too);
  • the admin's mailbox, which can be used to reset the WordPress password.

Then generate new security keys for wp-config.php from the official WordPress generator and replace the block with AUTH_KEY, SECURE_AUTH_KEY and the rest. Everyone signed in, the attacker included, is logged out.

Scan the computer you manage the site from with an antivirus as well: FTP and wp-admin passwords can be stolen from there too, by malware.

Step 3: scan

Use several sources, because each sees something different:

  • your host's malware scanner, which sees every file in the account;
  • a security plugin with a scanner, for example Wordfence or Sucuri Security, which compares the WordPress files with the originals;
  • an external scanner such as Sucuri SiteCheck, which sees the site as a visitor does.

Write down the flagged files and the dates they were modified. The date helps you pick the right backup.

The panel's Virus Scanner page with a scan result

Step 4: restore from backup or clean by hand

If you have a backup from before the infection, restoring is the fastest and safest option. The catch is that infections often stay hidden for weeks before anyone notices, so yesterday's backup may already be infected. You need backups at least a month or two old. After restoring, go straight to step 6: the hole exists in the clean copy too.

Without a suitable backup, clean by hand:

  1. Replace the wp-admin and wp-includes folders with a fresh copy of the same version from wordpress.org. Delete the old ones first: attackers add new files, which a plain overwrite doesn't remove.
  2. Reinstall every plugin and theme from its official source. Delete the plugins and themes you don't use, especially "nulled" ones (pirated copies of paid products), which often come with malicious code built in.
  3. There should be no .php files in wp-content/uploads. If you find any, they are almost certainly backdoors.
  4. Check the wp-content/mu-plugins folder: plugins there run automatically and don't show up in the usual plugin list.
  5. Read wp-config.php, index.php, .htaccess (on Apache) and the theme's functions.php: unreadable code with eval, base64_decode or gzinflate is suspicious.
  6. In the database, check siteurl and home in wp_options and search for <script in wp_posts.
  7. In wp-admin, under Users, delete unknown administrator accounts.

Step 5: scan again and change the passwords once more

After the cleanup, run the scans from step 3 again. Then change the passwords a second time: if the attacker read the new passwords before the cleanup, they still have them.

Step 6: close the hole

  • Update WordPress, every plugin and the theme. Many hacks use vulnerabilities that already have a fixed version available.
  • Turn on two-factor authentication for administrators; see the guide to 2FA in WordPress.
  • Turn off the file editor in wp-admin by adding define( 'DISALLOW_FILE_EDIT', true ); to wp-config.php.
  • Give the administrator role only to people who really need it.

Step 7: request a Google review

If Google flagged the site, open the Security issues report in Search Console. Once every listed issue is fixed, click Request Review and describe what you did. Google says reviews for hacked sites take from a few days to a few weeks. Don't resubmit while a request is pending.

Frequently asked questions

Can I delete everything and reinstall WordPress?

You can reinstall the core, the plugins and the theme, but the content (posts, pages, orders) lives in the database and in wp-content/uploads. Those you need to check and keep.

How do I find out how they got in?

Compare the dates of the infected files with the server's access logs for the same day: requests to a particular plugin or successful wp-admin logins usually show the way in.

On AYT hosting

On AYT's paid plans, malware cleanup and supervised WordPress updates are included. The server has a virus scanner, a web application firewall (ModSecurity), file protection, login rate limiting, IP blocking and CrowdSec. Each account runs its own PHP, isolated from the other accounts. Backups are daily for the last 7 days, weekly for 4 weeks and monthly for 24 months, kept in 3 locations, so you find a copy from before the infection even if the attack was noticed late. You restore the whole site or specific files from the panel. See the full list under features and the plans on the WordPress hosting page.

More articles

All articles