How to Fix a Hacked WordPress Site (Including the Redirect Hack)
Removing the malware is the easy part. If you do not find how they got in, you will be doing it again next week.
Updated 29 August 2026
The most common WordPress compromise redirects visitors — often only visitors arriving from Google, and often only on mobile — to a spam site. Because it hides from logged-in administrators and direct visits, it can run for weeks before anyone notices.
Cleaning it is straightforward. Not getting reinfected is the actual job, and it depends entirely on finding the entry point.
1. Contain it
- Put the site in maintenance mode, or take it offline if you can afford to.
- Take a full backup now, infected as it is. You will want it for forensics, and to recover anything you delete by mistake.
- Change all passwords: WordPress admins, hosting panel, SFTP/SSH, and the database.
- Rotate the salts in
wp-config.phpusing the official generator. This logs everyone out, including the attacker's session.
2. Find the entry point before cleaning
This is the step people skip, and it is why sites get reinfected. Look for:
- Out-of-date plugins and themes, especially any with a known vulnerability. Check their changelogs for security releases you missed.
- Unexpected admin users in Users → All Users. Note the registration date — it tells you roughly when they got in.
- Access logs around that date. Look for POSTs to unusual paths, especially in
wp-content/uploads/. - Recently modified files. On a server you control:
find . -type f -mtime -14 -name "*.php". Anything modified recently that you did not change is suspect. - PHP files in the uploads folder. There should be none, ever. A PHP file under
wp-content/uploads/is a backdoor until proven otherwise.
3. Clean
- Replace core. Download a fresh copy of your WordPress version and overwrite
wp-adminandwp-includesentirely. Never overwritewp-content. - Reinstall every plugin and theme from source rather than trying to clean them. Delete and reinstall — do not patch.
- Check
wp-config.php,.htaccessandindex.phpfor injected code. Redirect hacks very often live in.htaccessas a conditional rule that only fires for search-engine referrers. - Check the database. Look in
wp_optionsfor injected scripts insiteurl,home, and any custom header/footer option. Then checkwp_postsfor injected<script>tags. - Check scheduled tasks. Attackers add cron events that reinstall the payload after you clean it. A cron-viewing plugin will list them; anything unfamiliar goes.
- Delete unknown admin users — after you have noted their creation dates.
4. Verify
- Test as a search visitor, not as yourself: use a private window and arrive via a Google result, on mobile. That is the exact condition many redirect hacks trigger on.
- Scan with a reputable security plugin, and separately with an external scanner so you are not trusting a single source.
- Check Search Console → Security Issues, and request a review if the site was flagged.
- Compare your site's source against a known-good copy if you have one.
5. Close the door
- Update everything, and remove every plugin and theme you are not using — inactive code is still reachable.
- Add
define( 'DISALLOW_FILE_EDIT', true );towp-config.php. - Block PHP execution in
wp-content/uploads/at the server level. - Enforce strong passwords and two-factor for every administrator.
- Set up file-integrity monitoring so the next change is noticed in hours, not weeks.
- Make sure backups are automated, off-site, and that you have restored one at least once as a test.
When to bring in help
If the site handles payments or personal data, if you cannot find the entry point, or if it reinfects after a clean, get a professional incident-response service. The cost of a second compromise on a commercial site is far higher than the clean-up fee — and with personal data involved there may be disclosure obligations that are not optional.
Common questions
Why do only Google visitors get redirected?
The malware checks the referrer and the user agent, and fires only for search referrers or mobile browsers. That keeps it hidden from the site owner, who normally arrives directly and is logged in.
Can I just restore a backup?
Only if you are confident the backup predates the compromise, and only if you then patch the vulnerability that let them in. Restoring to a still-vulnerable state gets you reinfected quickly.
How did they get in?
Most often an out-of-date plugin with a public exploit. Next most common are weak or reused admin passwords, and compromised hosting or SFTP credentials.