Home > WordPress Security > How to Remove Malicious Code From WordPress Files

How to Remove Malicious Code From WordPress Files

A hacked WordPress website is more than a technical inconvenience. It can redirect potential customers to unsafe pages, damage search visibility, slow down checkout, expose customer data, and cost your business real leads. 

Knowing how to remove malicious code from WordPress files helps you contain the issue quickly, but the right cleanup process matters just as much as the removal itself.

remove wordpress malware

How to Remove Malicious Code From WordPress Files

#1 Contain the Problem

Your first goal is to prevent further damage. Put the website into maintenance mode if possible, or temporarily restrict access while preserving a copy for investigation. 

If the website is actively sending spam, redirecting visitors, or processing customer payments, ask your web host to suspend public access at the server level until you understand what has happened.

Before editing anything, create a full backup of the current site. This includes WordPress files, the database, and server logs if they are available. 

check error logs
malware scanning wordpress files

#2 Confirm Where the Malware Is Hiding

Malicious code can live in more places than a single WordPress file. Attackers often inject code into theme files, plugin files, the uploads folder, WordPress core files, database options, scheduled tasks, or the wp-config.php file. Some infections also create hidden administrator accounts or modify .htaccess rules to redirect visitors.

Start with a reputable malware scanner and review its findings, but do not treat an automated scan as final proof that the site is clean. 

Suspicious code may include long unreadable strings, unfamiliar external domains, encoded content, unexpected PHP files inside wp-content/uploads, or functions such as eval, base64_decode, gzinflate, and str_rot13. 

#3 Replace WordPress Core Files

Download a fresh copy of the same WordPress version from the official source. Replace the core directories and files, especially wp-admin, wp-includes, and root WordPress files. Do not overwrite wp-content or wp-config.php until they have been reviewed separately, since they contain your content, settings, themes, and plugins.

Replacing core files is generally faster and more dependable than comparing hundreds of files manually. It also removes altered core files that may not appear suspicious at first glance.

troubleshooting wordpress website down
wordpress themes

#4 Reinstall Themes and Plugins From Clean Sources

Delete and reinstall every plugin and theme from a trusted original source. For premium extensions, download fresh copies from the developer or authorized marketplace. 

Remove anything unused. Inactive plugins and themes can still contain vulnerable code, so keeping them “just in case” creates unnecessary risk. Retain one current default WordPress theme for troubleshooting, but remove old themes that have no business purpose.

If your site uses a custom theme or custom plugin, compare it against a known clean repository or backup. A web developer should inspect custom code carefully rather than deleting it wholesale. This is one of the few situations where manual review is necessary.

#5 Inspect the Uploads Folder and Root Files

The uploads folder should mostly contain media files such as images, PDFs, videos, and documents. PHP scripts in this directory are a major warning sign unless your development team has a documented reason for them. Review unfamiliar files before removing them, then delete confirmed malicious scripts.

Also inspect root-level files, including .htaccess, index.php, wp-config.php, xmlrpc.php, and any unfamiliar file with a random name. Attackers often add backdoors with innocent-looking names, then use them to regain access after an initial cleanup.

Check .htaccess for redirect rules you did not create. A legitimate WordPress .htaccess file is usually short. 

recover/restore your wordpress site from backup
WordPress database cleaning

#6 Clean the Database and Scheduled Tasks

Not all malware sits in files. Spam links and redirects can be stored in WordPress posts, widgets, plugin settings, the wp_options table, or scheduled cron events. Review recently edited pages, posts, menus, and widget areas. Search the database for suspicious domains, injected scripts, hidden iframes, or unfamiliar administrator accounts.

Check scheduled tasks as well. A malicious cron job can reinfect your website after the files have been cleaned. Remove tasks created by unknown plugins or scripts, but avoid deleting legitimate eCommerce, backup, form, or marketing automation tasks without confirming their purpose.

Close the Security Gap That Allowed the Attack

A cleaned website is only useful if the attacker cannot return. Most WordPress infections begin with outdated software, weak credentials, pirated extensions, excessive user permissions, or insecure hosting configurations. Your cleanup should end with practical hardening work.

Use this checklist to reduce the chance of reinfection:

  • Update WordPress core, themes, plugins, PHP, and server software to supported versions.
  • Remove unused plugins, themes, user accounts, and old SFTP or FTP credentials.
  • Enforce strong unique passwords and enable two-factor authentication for administrator accounts.
  • Set secure file permissions and disable direct file editing from the WordPress dashboard where appropriate.
  • Install a web application firewall and configure malware scanning, login protection, and alerts.
  • Schedule daily backups that are stored separately from the live hosting account.
 

Your web host also plays a role. If the server is running obsolete software, accounts are not properly isolated, or suspicious activity cannot be investigated through logs, a cleaner WordPress installation may still be exposed. For business websites, managed hosting and ongoing maintenance are often less expensive than repeated emergency cleanup work.

Most Popular

Need Website Help?

Get an affordable quote for services like website maintenance, website repair, website upgrade, & website malware removal. 

On Key

Related Posts