Why malware hides in the database
Most cleanups focus on files: replace WordPress core, reinstall plugins, run a scanner. Attackers know this. Code stored in the database survives all of those steps, and it’s often what puts the file malware back afterwards.
There’s also a practical reason. WordPress prints a lot of database content directly into your pages: post content, widgets, theme settings, and header and footer scripts. A single <script> tag saved in the right place appears on every page of your site without touching a single file.
Where malware hides in the WordPress database
| Table | What can hide there |
|---|---|
wp_posts |
Scripts, iframes or hidden spam links added to post and page content; thousands of spam posts or pages |
wp_options |
Scripts in widgets, theme settings and “insert header code” plugins; a changed siteurl or home (instant redirect); malicious scheduled tasks in the cron option |
wp_users / wp_usermeta |
Administrator accounts the attacker created, sometimes hidden from the Users screen |
wp_postmeta |
Code stored in page builder data or custom fields |
wp_comments |
Spam links and scripts in comments |
| Plugin tables | Anything a vulnerable plugin lets the attacker write |
Your tables may use a different prefix than wp_. You can find it in wp-config.php ($table_prefix) or by running wp db prefix.
How to check your database for malware
These queries only read data, so they’re safe to run in phpMyAdmin’s SQL tab or with wp db query. Replace wp_ with your prefix.
-- Scripts and iframes in posts and pages
SELECT ID, post_title, post_type, post_status FROM wp_posts
WHERE post_content LIKE '%<script%' OR post_content LIKE '%<iframe%';
-- Suspicious code in options (widgets, theme settings, header scripts)
SELECT option_name, LENGTH(option_value) AS size FROM wp_options
WHERE option_value LIKE '%<script%' OR option_value LIKE '%eval(%'
OR option_value LIKE '%base64_decode%' OR option_value LIKE '%fromCharCode%';
-- Site address (should be your own domain)
SELECT option_name, option_value FROM wp_options WHERE option_name IN ('siteurl', 'home');
-- Every administrator, with registration date
SELECT u.ID, u.user_login, u.user_email, u.user_registered FROM wp_users u
JOIN wp_usermeta m ON m.user_id = u.ID
WHERE m.meta_key = 'wp_capabilities' AND m.meta_value LIKE '%administrator%';
-- Sudden bursts of new posts (spam injections show up as spikes)
SELECT DATE(post_date) AS day, COUNT(*) AS posts FROM wp_posts
GROUP BY day ORDER BY posts DESC LIMIT 10;
Not every match is malware. Your analytics code, a chat widget or an embedded video will show up too. Look for scripts loading from domains you don’t recognize, long strings of encoded characters, or String.fromCharCode and eval, which legitimate content rarely needs.
If you’ve found strange entries in wp_options (huge values, random-looking option names, or anything containing base64 or eval), that’s usually the sign of a hacked wp_options table. It’s one of the most common places to clean an infected database, and one of the easiest to break if you edit it carelessly.
Why you shouldn’t just “find and replace” in phpMyAdmin
WordPress stores many settings as serialized data. That’s a format that records the length of every piece of text, like s:24:"...". If you remove a script from inside a serialized value with a plain SQL REPLACE, the recorded length no longer matches. WordPress then can’t read the value at all, and your widgets, theme settings or page builder layouts quietly disappear.
The safe ways to remove database malware are:
- Edit the affected post or widget in the WordPress dashboard, where possible.
- Use
wp search-replace, which handles serialized data correctly. Run it with--dry-runfirst. - Delete whole malicious records (spam posts, fake admins) instead of editing inside them.
Always export the database before changing anything: wp db export before-cleanup.sql.
How I clean a WordPress database
- Back up the full database before any change.
- Search every table, not just posts: options, user capabilities, postmeta, comments, and plugin tables.
- Separate legitimate from malicious. Your real tracking codes and embeds stay; the injected code goes.
- Remove safely, using serialization-aware tools or by deleting whole malicious records.
- Clean the source, too. Database malware is usually put there by a backdoor in the files, so the files get the full cleanup as well.
- Rotate the database password and security keys, then verify the site and give you a report.
A typical case: a blog owner cleaned the files twice with a security plugin, but visitors kept seeing a fake “click Allow to continue” page. All the files checked out clean. The database told a different story: the same 300-character script had been appended to the end of over a thousand posts, plus a copy in a text widget. A small backdoor in the uploads folder re-added it whenever it was removed. Removing the script from every post with a dry-run-tested wp search-replace, deleting the widget code and the backdoor, and updating the vulnerable form plugin fixed it in one go, without losing a single post.
Database malware is usually part of a bigger infection
It’s rare for an infection to live only in the database. Something wrote it there, and that something is usually still on the server. If you’d like to tackle it yourself, follow my step-by-step WordPress malware removal guide. If you’d rather have it done properly the first time, see the full malware removal service.