
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)
add_filter('widget_text','check_404',100); 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.