WordPress keeps getting hacked: what to do and how to stop it
A site that gets reinfected was never fully cleaned. How to contain it, remove backdoors, change every login, close the way in and lift Google's warning.
By Sohail Hussain21 November 20266 min read
If a WordPress site keeps getting hacked after being cleaned, the cleanup missed something. Usually it is one of four things: a backdoor file the attacker left behind, a hidden administrator account, the original vulnerable plugin still installed, or credentials (hosting, SFTP, database or an admin login) that were never changed. Sometimes the infection comes from another site in the same hosting account. Stopping it takes a complete clean, not a scan-and-delete: replace all code from fresh copies, remove what should not be there, change every password and key, close the way in, and then watch.
If you want this handled for you, malware cleanup and hardening is part of our website care and maintenance work. If you are doing it yourself, follow the steps below in order.
Why does a cleaned site get hacked again?
Malware scanners find the visible infection: the spam pages, the redirect script. Attackers expect that, so they leave more than one way back in.
- Backdoors. Small PHP files that let them run code, often hidden in
wp-content/uploads, inwp-content/mu-plugins, or added to a genuine plugin file. One missed file is enough. - Rogue administrators. An account with an innocent name, such as "wpsupport", created during the first attack.
- The same hole. If the vulnerable plugin is still installed, the bots that found it the first time will find it again within days.
- Unchanged credentials. If the attacker has the hosting or SFTP password, perhaps from malware on an office computer, cleaning WordPress achieves nothing.
- Neighbours. Sites in the same shared hosting account can reinfect each other. A forgotten staging copy or an old install in a subfolder counts.
What should you do in the first hour?
Write down what you see. The official FAQ for hacked WordPress sites starts here: what is wrong, when you noticed, and what changed recently, such as a new plugin, a theme edit or a new user. This becomes your incident record.
Take a snapshot of the infected site. Copy the files and the database before you change anything. You need them to work out what happened, and as a fallback if the cleanup goes wrong.
Tell your host. They may already know, may have suspended the site, and can often see from their logs which files changed and when.
Change the hosting, SFTP and database passwords from a computer you trust, and run a malware scan on your own computers. Our password generator makes long, unique passwords in the browser.
Do not restore last night's backup and call it done. It probably contains the backdoor, and it certainly contains the hole.
How do you clean the site properly?
Replace core. Download the same WordPress version fresh and replace the wp-admin and wp-includes folders entirely. Deleting and replacing matters, because attackers add files rather than only editing existing ones. Then check what is left with WP-CLI:
wp core verify-checksums
wp plugin verify-checksums --all
These compare your files with the official copies on WordPress.org and list modified or unexpected files. Premium plugins are not hosted on WordPress.org, so they cannot be checked this way. Delete them and reinstall from the vendor's own download.
Reinstall every plugin and theme from fresh copies, never from the infected folders. Delete anything you do not use.
Look for PHP where it should not be. The uploads folder should hold images and documents, never PHP:
find wp-content/uploads -type f -name "*.php"
Also check wp-content/mu-plugins (must-use plugins load automatically and do not appear in the normal plugin list), the .htaccess file in the root and any in subfolders, wp-config.php for extra code at the top or bottom, and the theme's functions.php, header.php and footer.php.
Check users and scheduled tasks.
wp user list --role=administrator
wp cron event list
Remove any administrator nobody can account for, and any scheduled event whose hook belongs to no installed plugin.
Check the database. Search posts and options for injected script tags and spam links, check that the siteurl and home options hold your real address, and look at options added recently by no known plugin.
Reset every key. New security salts end every login session, including the attacker's:
wp config shuffle-salts
Then reset every user's password. Once the site is confirmed clean, change the passwords again; the official FAQ asks for this second change for good reason.
Stop PHP running from uploads. On Apache, put a .htaccess file inside wp-content/uploads with this rule (Apache 2.4 syntax):
<FilesMatch "\.php$">
Require all denied
</FilesMatch>
On Nginx, the equivalent is a location rule in the server configuration; ask your host to add it.
How do you close the door it came in through?
Cleaning without finding the entry point is how sites end up hacked again. Look at:
- File dates. The oldest malicious file's modification time narrows the window. Check the server's access logs around that time for requests to a particular plugin's files, or for a successful login from an unfamiliar address.
- Known vulnerabilities. Check each plugin and theme version you had against a public vulnerability database. If one had a published flaw at the time, that is the likely way in.
- Credentials. Were there shared admin logins, a former employee's account still active, or SFTP passwords saved in an FTP app?
Then fix it. Update or replace the vulnerable plugin. Turn on two-factor login for every administrator and give everyone else a lower role. Move each site into its own hosting account. Switch off the dashboard file editor. WordPress security: the ten things that stop most attacks lists the rest.
How do you get Google's warning removed?
If Google has flagged the site, the Security Issues report in Search Console lists what it found. Once every issue is fixed across the whole site, request a review from that report and describe what you fixed. Google says reviews for hacked sites take from a few days to a few weeks, per its Security issues report documentation. Do not resubmit while a review is pending.
Also check whether spam pages the attacker created are still indexed by searching site:yourdomain.com with a few spam terms, and remove them or let them return a 404. If the site sent spam email, ask your host whether the server's IP address has been blocklisted.
When should you rebuild instead of cleaning?
Clean when the site is reasonably up to date, you know what is installed and why, and this is the first infection.
Rebuild, by moving the content into a fresh installation, when:
- It is the third reinfection and the entry point is still unknown.
- The site runs nulled (pirated) themes or plugins.
- WordPress, PHP or key plugins are years out of date, and updating them would break the site anyway.
- Nobody can say what half the plugins do.
A fresh install with the content imported from a WordPress export file, checked for injected scripts, plus a clean theme and only the plugins you need, is often cheaper than a fourth cleanup. Your website got hacked: a recovery plan covers the steps that apply to any platform, and WordPress for business websites describes the setup that keeps it from happening again.
Was this guide useful?