Hive Manila

How to clean a hacked WordPress site, in the right order

The clean-up steps we use on a hacked WordPress site, in the order that keeps attackers from walking back in. Every command is ready to copy into cPanel's Terminal.

Developer placing files into a storage box at night beside a laptop

This is Part 3 of our hacked WordPress series. Part 1 is the story of a site that got hacked again after a restore. Part 2 is how to tell if your site is hacked. This part is the clean-up.

The order matters more than any single step. Most failed clean-ups do the right things in the wrong order: they delete files first, while the attackers still have a working password, so the attackers simply come back and plant everything again.

1 2 3 4 5 6 Save a copy Files and database, as they are now Lock them out Passwords, admins, sessions, keys Quarantine Move planted files out, don't delete Reinstall WordPress, plugins, themes, official copies Block and harden No PHP in uploads, no file editor, 2FA Watch Two weeks of checks
Step 2 before step 3. Clean while they still have a key and they'll be back by morning.

You’ll need cPanel access and its Terminal, like in Part 2. Each command has a copy button. Start in your website’s folder:

cd ~/public_html

If you’d rather not do this yourself, send us the site. It’s the same work we’d do, and you’ll get a plain-words report of what we found.

1. Save a copy of everything

If you ran Part 2, you already have this. If not:

mkdir -p ~/incident ~/quarantine
tar -czf ~/incident/files-$(date +%F).tar.gz -C ~/public_html .
wp db export ~/incident/database-$(date +%F).sql

This also creates a quarantine folder in your home folder, outside the website. We’ll move suspicious files there instead of deleting them.

2. Lock them out

Do all of this before touching a single file.

Find every admin:

wp user list --role=administrator --fields=ID,user_login,user_email,user_registered

Demote any admin you don’t recognise (replace USERNAME). Demoting keeps the account as evidence but takes away its power. Their old posts stay where they are.

wp user set-role USERNAME subscriber

Reset the password of every remaining admin. This sends each admin a reset email, so make sure the email address on each account is one you control:

wp user reset-password $(wp user list --role=administrator --field=user_login)

Log everyone out, everywhere:

wp user session destroy --all
wp config shuffle-salts

The second command changes WordPress’s secret keys, which ends every session, including any the attackers kept.

Close public sign-up if your site doesn’t need it:

wp option update users_can_register 0
wp option update default_role subscriber

Change the passwords outside WordPress too: your cPanel or hosting login, any FTP accounts, and the email account that receives the admin reset emails. Change the email password first. If the attackers can read that inbox, they can reset everything else.

3. Move planted files into quarantine

Use your notes from Part 2. Move each suspicious file or folder out of the website instead of deleting it. For example:

mv wp-content/plugins/seo_1794290843 ~/quarantine/
mv wp-logiin.php ~/quarantine/

Use your own file names; those two are from the site we cleaned. Do the same for:

  • PHP files inside wp-content/uploads. First list them and note where each one was, then move them all. Files with the same name get numbered, so nothing is overwritten:
find wp-content/uploads -name "*.php" ! -name "index.php" | tee ~/incident/uploads-php-list.txt
find wp-content/uploads -name "*.php" ! -name "index.php" -exec mv --backup=numbered -t ~/quarantine/ {} +
  • Extra .htaccess files in folders that shouldn’t have one. The main folder’s .htaccess is normal. One in every plugin folder, all changed on the same day, is not. Look at each before moving it:
find . -name ".htaccess" -not -path "./.htaccess" -printf '%TY-%Tm-%Td  %p\n' | sort
  • Google verification files you didn’t add (google....html in the main folder).
  • Cron jobs and SSH keys you didn’t add. Remove a bad cron job with crontab -e, and a bad key by editing ~/.ssh/authorized_keys in the File Manager.

Then check the site still loads. If something broke, the file you need is in ~/quarantine, not gone.

4. Reinstall from official copies

You won’t find every changed file by hand. Replacing them all is faster and safer.

WordPress itself, same version you have now, without touching your content:

wp core download --force --skip-content --version=$(wp core version)
wp core verify-checksums

Every free plugin, from WordPress.org:

wp plugin list --field=name | xargs -n1 wp plugin install --force
wp plugin verify-checksums --all

This installs the latest version of each plugin, so it doubles as an update. Paid plugins will show an error here. That’s expected: download a fresh copy from the seller’s website, then upload it in WordPress under Plugins, Add New, Upload.

Themes: reinstall free themes the same way (wp theme install THEMENAME --force). For a paid or custom theme, get a fresh copy from the seller or your developer. If your theme was edited directly, those edits will need to be added back, which is a good moment to move them into a child theme.

Remove what you don’t use. Every inactive plugin and theme is one more door:

wp plugin list --status=inactive
wp theme list --status=inactive

Delete the ones you don’t need from the WordPress dashboard.

5. Block and harden

Stop PHP from running in the uploads folder. Images and documents keep working; code planted there won’t run:

cat > wp-content/uploads/.htaccess <<'EOF'
<FilesMatch "\.(php|phtml|phar|php[0-9])$">
  Require all denied
</FilesMatch>
EOF

This works on Apache and LiteSpeed, which most cPanel hosts use. If you’re not sure, ask your host.

Turn off the built-in file editor, so an admin login can’t be used to edit plugin code from the dashboard:

wp config set DISALLOW_FILE_EDIT true --raw

Turn on two-step login for every admin. A free security plugin can add it. A stolen password alone is then not enough to get in. On the site in Part 1, this one change would have stopped the whole attack.

Keep updates on. Don’t switch off plugin updates out of fear of breakage. Keep a backup, and check the site after updates instead. We explain why in your WordPress site went down, and nobody hacked it.

6. Watch for two weeks

If the attackers kept a way back in that you missed, it usually shows within days. Every few days for two weeks, run the key checks from Part 2 again:

wp user list --role=administrator --fields=user_login,user_registered
find . -name "*.php" -mtime -3
find wp-content/uploads -name "*.php"
wp core verify-checksums

New admins, new PHP files or failed checksums mean something was missed. Go back to step 2.

Also open Google Search Console. Under Settings, Users and permissions, remove any owner you don’t recognise. Under Security and manual actions, check for warnings. If Google flagged the site, request a review once it’s clean.

When to stop and call someone

Call someone if any of these happen:

  • The site is hacked again after you’ve done all six steps.
  • Customer data, payments or accounts may have been exposed. You may have a duty to tell people, and it’s worth getting it right.
  • You find code you don’t understand in the database, not just in files.
  • You simply don’t have the time. That’s a good reason too.

Send us the site and we’ll clean it, tell you what happened in plain words, and set it up so it doesn’t happen again. If you’d like it watched every month after that, that’s what our care plans are for.

Next in the series: where backdoors hide, and why one missed file can undo a whole clean-up.

Sources

Common questions

What is the first thing to do when a WordPress site is hacked?

Make a full backup of the site as it is, then lock the attackers out: reset every admin password, remove admin accounts you don't recognise, end all logged-in sessions and change WordPress's secret keys. Clean the files after that, not before.

Can I just delete the hacked files?

Move them to a folder outside your website instead. You may need them to understand how the attackers got in, or to undo a mistake if you moved a file the site needs.

Should I reinstall WordPress after a hack?

Yes. Reinstall WordPress itself, every plugin and every theme from official copies. That replaces any file the attackers changed without you having to find each one. Paid plugins need a fresh download from the seller.

How do I stop hackers from running code in my uploads folder?

Add a small .htaccess file inside wp-content/uploads that refuses to run PHP files. Images and documents keep working. The exact file is in this guide.

How long should I watch the site after cleaning it?

At least two weeks. Check for new admin accounts, new PHP files and logins from unfamiliar places. If the attackers kept a way back in, it usually shows within days.

Get the Sunday read

One short email a week with our newest insights and case studies. Sunday, 8 AM Manila time.

Rather have us clean it?

A short brief, then a written proposal within 1 working day. We talk once the work needs it.