Disable Adsense ad code in 404 deleted posts and pages
recently I got deleted some of older posts, now AdSense crawler throws a continuous error so that I used a code to block AdSense on 404 pages,
I found an online solution,
The php-code snippet which hides/blocks Adsense ads on 404’s is:
<?php if (!is_404()) { ?>
ADD HERE YOUR ADSENSE CODE
<?php } ?>
Note: the logic of the code is to let Adsense run on all pages but the 404s!
And add below code into functions.php,
add_filter(‘widget_text’,’execute_php’,100);
function execute_php($html){
if(strpos($html,”<“.”?php”)!==false){
ob_start();
eval(“?”.”>”.$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
(this code throws error —- Your PHP code changes were rolled back
due to an error on line 123 of file
wp-content/themes/generatepress/functions.php. Please fix and try
saving again.syntax error, unexpected ‘)’
Leave an answer