Simply put the following code in you wordpress functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?php /** * PRECISE ADSENSE REMOVAL USING OUTPUT BUFFERING * Safely removes only AdSense code for post ID 105320 */ function buffer_start_adsense_removal() { if (is_single(105320)) { ob_start('safely_remove_adsense_from_output'); } } add_action('template_redirect', 'buffer_start_adsense_removal', 1); function buffer_end_adsense_removal() { if (is_single(320, 22, 24)) { if (ob_get_length()) { ob_end_flush(); } } } add_action('shutdown', 'buffer_end_adsense_removal', 999); function safely_remove_adsense_from_output($buffer) { // 1. Remove specific AdSense loader scripts (more precise patterns) $patterns = [ // Your specific async script (very precise match) '@<script\s+async\s+src=["\']https://pagead2\.googlesyndication\.com/pagead/js/adsbygoogle\.js\?client=ca-pub-xxxxxxxxx["\']\s+crossorigin=["\']anonymous["\']>\s*</script>@i', // Standard AdSense script pattern (more precise) '@<script\s+(?:async\s+)?src=["\']https?://pagead2\.googlesyndication\.com/pagead/js/adsbygoogle\.js(?:\?[^"\']*client=ca-pub-xxxxxxxxx[^"\']*)?["\']\s*(?:crossorigin=["\'][^"\']*["\'])?\s*>\s*</script>@i', // Auto Ads initialization (only matches the push code) '@<script>\s*\(adsbygoogle\s*=\s*window\.adsbygoogle\s*\|\|\s*\[\]\)\.push\s*\({[^}]*}\);\s*</script>@i', // Ad units (only matches ins elements with specific class) '@<ins\s+class="adsbygoogle"[^>]*>(?:.*?</ins>)?@is', // Noscript fallbacks (only when adjacent to adsbygoogle) '@<noscript>\s*<ins\s+class="adsbygoogle"[^>]*>.*?</ins>\s*</noscript>@is' ]; foreach ($patterns as $pattern) { $buffer = preg_replace($pattern, '', $buffer); } return $buffer; } ?> |
Not: Replace pub-xxxxxxxxx with your publisher id and replace 320, 22, 24 with your post id’s. These 3 ids showing three wordpress post.