75 guides published Start a blog Fix my site How we test
SECURITY

How to Find a Backdoor in functions.php

A theme’s functions.php runs on every request and is rarely read after the site is built, which makes it an attractive place to hide code. Injected blocks usually sit at the very bottom, after a large gap of blank lines, so a casual scroll shows an apparently normal file.

The short version

  • Scroll to the absolute end of the file, not to the end of the visible code.
  • Obfuscation is the tell. Legitimate theme code is readable.
  • Check mu-plugins and uploads at the same time.
  • If one site is affected, check every site sharing that server or that theme.

What injected code looks like

Four patterns cover most of what you will encounter:

  • Encoded payload. A long base64 or hex string passed through a decode function and then executed.
  • Dynamic function calls. Function names assembled from string fragments at runtime so a plain search does not find them.
  • Remote fetch. Code that retrieves instructions from an external URL on each request.
  • Hidden administrator. A function hooked to an init action that silently creates or restores a user account.

Finding it

Work from the filesystem, not the dashboard. If a backdoor is present it may be filtering what the dashboard shows you.

# functions.php files modified recently
find . -name functions.php -mtime -30 -ls

# common obfuscation markers across the install
grep -rn --include=*.php -E "eval\(|base64_decode|gzinflate|str_rot13|assert\(" .

# PHP in uploads, which should never happen
find wp-content/uploads -name "*.php"

# must-use plugins, loaded silently
ls -la wp-content/mu-plugins/

Watch out

Searching for the word eval alone produces false positives — some legitimate libraries use it. Judge by whether the code is readable and whether it belongs to a file you can match against an official release.

Confirming before you delete

Download a clean copy of the same theme version from its official source and diff it against what is on the server. Anything present on the server and absent from the clean copy is either a customisation you made or an injection. If you did not write it, it is the latter.

For a child theme you wrote yourself, compare against version control. This is the moment that keeping theme files in git repays the effort.

Removing it safely

1. Copy the file off the server first

You want the original for comparison if something recurs.

2. Remove the injected block entirely

Not just the payload — the whole function and its hook registration.

3. Search for a companion

Injections usually come in pairs: one that does the work and one that restores it. Look for scheduled events referencing unknown function names.

4. Check scheduled tasks

List cron events and remove anything you cannot account for.

5. Rotate salts and passwords

The backdoor may have exfiltrated credentials before you found it.

Then check everything else

A backdoor rarely affects a single site. If several of your sites share a server, a hosting account, a deployment pipeline or the same nulled theme, check all of them the same day. Searching for the exact string you found is the fastest first pass.

CheckScope
Same string searchEvery site on the same hosting account
Same theme or pluginEvery site using it, regardless of host
Same FTP or SSH credentialsEvery site those credentials can reach
Same developer machineCheck it for malware that steals stored credentials

Preventing the next one

  • Keep theme files in version control so unexpected changes are immediately visible.
  • Set DISALLOW_FILE_EDIT so the dashboard cannot write PHP.
  • Block PHP execution inside the uploads directory at the server level.
  • Run file-integrity monitoring that alerts on changes to PHP files.
  • Never install nulled themes or plugins.

Common questions

Can a scanner find these reliably?

Scanners catch known signatures well and novel obfuscation poorly. A diff against official files is more reliable than any signature database.

Why the bottom of the file?

Because it survives casual inspection. Many editors open a file at the top, and a large block of blank lines pushes the payload out of sight.

Is a child theme safer?

Slightly, in that core theme updates will overwrite an injection in a parent theme. It is not a defence — a child theme’s functions.php is equally writable.

S

Sumit

Writes everything on TechMozz.

More in Security