WordPress Hack Displays Google Ads on Every Page for Logged-Out Users – Symptoms & Solution

Header image with the post's title, along with a background image of servers

Recently we’ve seen a few cases of a new WordPress hack which manages to display Google ads on every page of a website, but only for logged-out users. You may also experience other mysterious symptoms such as pages being moved to the trash.

While it’s not particularly malicious, it’s still annoying and can undermine the credibility of your organization, in addition to revealing security problems with your website.

For these reasons, we strongly recommend you act immediately to secure your site.

Below, I will outline how the hack works, share some code examples, and then explain the solution to this frustrating problem.

*Note: as with any time you are making changes to your website, I strongly recommend backing up both your website files and database before proceeding with anything. If you mess something up, it may be your only method of restoring the site unless you have quality website hosting.

Description of this WordPress Hack

The most obvious symptom of this particular hack is that your website will display ads on every page, but only to logged-out users. This is a common technique used to prevent site owners from realizing that their website has been hacked, since they are typically logged-in and may not see the ads.

The code added to the site includes this line, which prevents anything from being added if the current user is logged-in:

Curiously, this code appears to be redundant. Basically, it checks to make sure the current user is neither logged-in nor an administrator. But you can’t be an administrator if you aren’t logged-in.

Often, this type of code is not written by truly-talented individuals, or potentially humans at all. It may have sort of “evolved” that way or been written by AI. But I digress.

We have found two specific sections of code, both of which lead to the same thing: Google ads in the footer for logged-out users. Here are both of those snippets of code.

This first one is pretty direct and leaves no question as to what it is trying to do.

This second one does the same thing, but is obfuscated by hosting the code at Github.

We found these snippets of code in two places:

  1. The end of core WordPress files in the site root (wp-config.php and wp-load.php, in our cases)
  2. The end of WordPress plugin files (custom plugins in our case, but it could be any plugin)

The result of both was the same. This script was added somewhere in the site footer, which then loaded Google ads:

Note that if you were to inspect the ads code, you would see something other than the above. That’s because the above code basically loads external code from Google, and then adds a bunch more HTML and other scripts to the site.

In addition to the Google ads, we also determined that, for whatever reason, this hack would randomly delete website pages and we’d find them in the trash. This caused problems with our menu and also some confusion when content seemed to go missing.

Solution to Google Ad Hack

The solution to this hack is similar to most hacks, with a few extra steps. In general, the first step is to secure the site, then restore things as-needed.

Note that if you have quality website hosting and can easily restore your site from a backup of a time when you know there were no issues, that’s probably your safest bet. Then you can simply update all of your plugins and run some security scans to prevent the hack from happening again.

However, if you don’t have that as an option (either because your hosting is bad or because too much has changed on your site to go back), here is how I would remove the hack.

  1. Replace all WordPress core files (Dashboard -> Updates -> Reinstall Version x.x.x should work, although doing it manually is better)
  2. Update all of your plugins, theme files, and WordPress itself to their newest versions (make sure you have active licenses to premium plugins and themes so that you can properly update them)
  3. Install and activate the WordFence plugin and run a scan of your site
  4. Once that has finished, review the results of the scan. Restore original versions of WordPress and core files and remove any unnecessary, infected files
  5. If any plugins have been identified as abandoned, you should probably deactivate and delete them (make other arrangements for their functionality, if necessary. Consider running a plugin audit to help you decide what to do)
  6. Search the content of all files in your site for “googlesyndication” and “gist.githubusercontent.com” – there are a variety of ways to search file contents. Perhaps your hosting has a tool for this. If you have no other options, you can use either SSH to search through the files, or download the site using FTP and search locally
  7. If you’ve found any instances of those strings, check to make sure this isn’t code that’s adding the ads. If it is, you’ll want to remove that part of the code
  8. Inspect all admin users on the website, and delete any that aren’t needed or are suspicious
  9. Reset all admin passwords to ensure attackers don’t have access via one of them
  10. Run one final WordFence scan to ensure your site is now secure
  11. Check the trash for pages and posts and restore any that should be active

Once you’ve completed all of these steps, you should have removed the code that adds the Google ads to your footer, in addition to the original vulnerability that led to your site being hacked.

One final note: I hesitate to add this, but I think it might help people.

You can hide the Google ads section simply by adding the following code to your theme’s style.css:

The only issue here is that if you do this, you might not even notice you’ve been hacked. It would simply hide the symptom.

But it might be a good idea to add this in if you are having trouble pinpointing where it’s coming from. Then at least nobody will see the ads until you’ve figured out how to solve it.

It’s quick and dirty but works!


Conclusion: this is far from the worst that could happen, so stay secure

As far as website hacks go, this one is actually pretty tame. It just displays legitimate Google ads, as opposed to offensive content or sending people to shady website and infecting them with malware.

Consider yourself lucky!

But you should take this as a warning. If you just got hacked, you need to start taking things more seriously.

In the future, you need to make absolutely sure that you are updating all plugins and themes and WordPress core files regularly. Delete abandoned plugins and themes. Regularly update passwords. Use a security plugin and quality, secure hosting.

This time it was just some harmless ads. Next time, your visitors may get hacked themselves or see wildly offensive content. Or worse! Their credit card information could be stolen and you could be liable for it.

If you need help dealing with this hack, or if you’d like us to review your site and make sure it is secure, please use the form to reach out below and we’ll reach out right away.




About Brian Johnson

Brian Johnson is a website developer and designer living in Minneapolis, Minnesota with a passion for code and WordPress. He spends his days building WordPress websites for small businesses, developing new code with the online community, and living life.

Leave a Reply

Your email address will not be published. Required fields are marked *