WordPress Theme Malware: How to Clean Infected Theme Files Without Losing Customizations

WordPress theme malware cleanup preserving customizations and replacing infected theme files safely

WordPress theme malware is especially risky because an infected theme can run code on every front-end request. A few injected lines in functions.php, header.php, or an included file may redirect visitors, recreate spam, add hidden administrators, or load a remote payload.

Replacing the whole theme may remove malware but also erase a child theme, custom templates, styling, or business logic. Preserve evidence and intentional work before replacing untrusted code.

RyoheiYokoyama

I’m Ryohei Yokoyama, founder of SiteFixNow. I’ve worked as an IT engineer for over 20 years and have handled many WordPress recovery, malware removal, hacked site repair, and security cleanup cases. In this article, I’ll explain how to clean infected theme files without sacrificing legitimate customizations or overlooking the wider compromise.

What you’ll learn
  • Which theme files deserve investigation first
  • How to preserve child-theme and custom work
  • How to compare a theme with a trusted clean package
  • How to replace infected files and verify the site safely
On This Page

WordPress theme malware: identify infection without guessing

Start by proving what changed instead of deleting every unfamiliar file. Themes legitimately contain PHP, JavaScript, templates, generated assets, and vendor libraries, so a strange name or long code block is not enough to declare malware.

Useful evidence combines timestamps, differences from the official package, unexplained remote requests, obfuscation, and behavior that stops when the theme is disabled. Record symptoms and recent updates first.

Theme locations to inspect first
  • wp-content/themes/active-theme/functions.php
  • header.php, footer.php, and templates
  • inc/, includes/, vendor/, and new folders
  • Inactive themes, copied themes, and old ZIP packages in public folders

Inventory installed themes and confirm the active parent and child. If the site has redirects or unknown scripts, review WordPress file infection cleanup across wp-content and wp-config.php, because the theme may be only one affected area.

wp theme list
find wp-content/themes -type f -mtime -14 -print
find wp-content/themes -type f \( -name "*.php" -o -name "*.js" \) -print

WordPress theme malware evidence: preserve files and customizations

Before cleanup, save an evidence copy and a separate working copy. This protects custom code and timestamps if replacement causes an error.

Preserving and inspecting infected WordPress theme files before cleanup

Preserve site files, database, logs, and both active theme folders. Treat the child theme’s overrides separately from parent-theme vendor code.

Preserve before changing anything
  1. Export the database and copy the current document root
  2. Archive the parent theme and child theme separately
  3. Save server logs covering the suspected infection window
  4. Record theme version, source, license, and recent manual edits

The newest backup may already contain malware. The guide to restore WordPress from a backup after a hack explains how to choose a restore point and verify it.

WordPress theme malware comparison: separate vendor code from custom work

Compare against a clean package from the original source and use the same theme version. Different versions create legitimate noise that can hide malware.

Comparing an infected WordPress theme with a clean package while protecting customizations

Replace a commercial parent theme instead of repairing vendor files by hand. For a custom or child theme, keep documented overrides, hooks, CSS, and approved snippets; quarantine unexplained code.

diff -ru clean-theme/ suspected-theme/ > theme-differences.txt
find suspected-theme -type f -exec shasum -a 256 {} \; > theme-hashes.txt

# Review differences; do not delete automatically.

Review unexpected decoding, remote includes, file-writing code, logged-out-only behavior, and PHP inside asset folders. These are warning patterns, not automatic proof.

Preserve unexplained differences outside the live web root before restoring anything.

WordPress theme malware cleanup: replace infected files in a controlled order

Remove the attacker’s write path first. Stolen credentials, a vulnerable plugin, or a backdoor can reinfect a clean theme.

Use maintenance mode, rotate access, and disable confirmed entry points. Replace the parent theme, restore only reviewed custom changes, clear caches, and test before reopening traffic.

Controlled theme replacement order
  1. Contain the incident and rotate compromised access
  2. Move the infected theme copy outside the public web root
  3. Install the clean parent theme from its trusted source
  4. Restore only reviewed child-theme and custom-theme changes
  5. Clear page, object, CDN, browser, and hosting caches

If replacement causes a fatal error, do not restore the infected folder wholesale. Check logs, PHP, and dependencies, then use the guide to recover a WordPress site after a plugin or theme crash.

WordPress theme malware scope: check beyond the theme folder

A clean theme does not prove a clean site. Investigate other files, accounts, database values, scheduled tasks, and server access.

Check wp-config.php, .htaccess, plugins, uploads, administrators, database options, and WP-Cron. Review server logs around the theme’s change time.

wp-config.php
.htaccess
wp-content/plugins/
wp-content/mu-plugins/
wp-content/uploads/
WordPress Users and application passwords
Database options and scheduled events
Hosting, SFTP, access, and error logs

Scanners may miss custom loaders, while file comparison may miss stolen access. Follow the wider process to scan WordPress for malware and hidden backdoors before declaring recovery complete.

WordPress theme malware verification: test behavior and watch for return

Prove both function and security. Test public pages, wp-admin, forms, checkout, mobile, logged-out visits, and search-result traffic.

Verifying a clean WordPress theme replacement across website server and database

Recheck hashes and timestamps after page loads and scheduled tasks. If code returns, stop deleting symptoms and find the process or account writing it.

Verification before you call the site clean
  • Theme files match the trusted package plus documented custom changes
  • Desktop, mobile, logged-in, and logged-out tests behave normally
  • No suspicious file returns after cache clearing or scheduled events
  • Access has been rotated and the original vulnerability is closed

Then secure WordPress after malware removal: update trusted components, remove abandoned themes, keep off-site backups, and monitor changes.

WordPress theme malware FAQ

Can I delete an infected WordPress theme and reinstall it?

Yes for a replaceable parent theme, but save evidence first. Review child-theme changes separately and check the wider site so a backdoor cannot infect the replacement.

How do I know whether code in functions.php is malware?

Compare the correct clean version with documented custom code. Remote calls, obfuscation, file-writing behavior, redirects, and unexplained changes require review.

Will switching themes remove the malware?

It may stop theme-only code, but it does not remove stolen accounts, database injections, cron persistence, or the original vulnerability. Switching is an isolation test, not proof of cleanup.

WordPress theme malware cleanup summary

Clean WordPress theme malware by preserving evidence, separating parent and child themes, comparing the correct clean version, replacing vendor code, and restoring only reviewed customizations.

Most importantly, verify the whole site. Rotate access, inspect other persistence locations, test from several visitor contexts, and monitor for the malicious file’s return. A theme replacement is complete only when the writer is gone and the site remains clean.

If You Can’t Secure or Recover Your WordPress Site Yourself

Ryohei Yokoyama, founder of Site Fix Now — WordPress site recovery, repair, defacement, malware removal and site hijacking specialist. Recovery in as little as 30 minutes.

If your website shows malware warnings, redirects to strange pages, or you are not sure whether it is secure,
SiteFixNow can help clean, repair, and recover your WordPress site.

Common problems we can help with
  • Your WordPress site may be infected with malware.
  • Security warnings appear in Google or browser results.
  • You found unknown admin users or suspicious files.
  • The site redirects to spam or unknown websites.
  • You need urgent WordPress hacked site repair.

We help with WordPress malware removal, hacked site repair, security cleanup, and recovery support.

Why ask for help early?
  • Reduce visitor risk and SEO damage.
  • Find hidden malware and backdoors, not only visible symptoms.
  • Recover the site safely without unnecessary data loss.

About the Author

Hello, I’m Ryohei Yokoyama, an IT engineer with over 20 years of experience.

I have received more than 776 reviews for WordPress recovery,
website repair, and online courses.

Many clients have shared comments such as:

“They restored my site so quickly!”
“They handled it the same day, which was a huge help!”

I am proud to have received a very high rating of 4.9 out of 5.0.

I have also published more than 30 books on WordPress, SEO, Microsoft Office, and related topics,
with multiple titles reaching No. 1 in sales rankings.

In addition, I have created more than 3,000 services, systems, and websites.

Through this experience, I have helped many people overcome technical problems, frustrations, and challenges.
Based on that practical perspective,
I explain complex topics in a clear and easy-to-understand way.

On This Page