How To Avoid Adsense on WordPress 404 pages


In 9 cases out of 10, you’re probably using the custom HTML Widget to add your Adsense code onto your WordPress based site.

That’s fine but it comes with a caveat. What to do when someone navigates to your nicely dressed up 404 page?

The answer is quite simple; we add a filter to widget_text that checks if the page is a 404 page, if so we pass back an empty response. Voilá.

In code (in your templates functions.php file)

function check_404($html){
    if (is_404()){
        return '';
    return $html;

The above code will hook into the rendering of the adsense code hosting widget, and use the nifty wordpress function is_404() to check whether or not the visitor landed on a 404 page and if that is the case, we send back an empty string, instead of the $html that contains the adsense code.

Simple as pie.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.