Skip to content

Instantly share code, notes, and snippets.

@seemly
Created March 28, 2023 10:57
Show Gist options
  • Save seemly/37a284d220aa5513e2b5e6ee5e72a835 to your computer and use it in GitHub Desktop.
Save seemly/37a284d220aa5513e2b5e6ee5e72a835 to your computer and use it in GitHub Desktop.
Conditionally hide Google Adsense snippet on a WordPress site using WP Code and ACF
<?php
// Check if ACF function `get_field()` exists. If it doesn't exist, display ads won't be shown.
// If it does exist and ACF 'hide_display_ads' value is not falsey, show display ads.
if(function_exists('get_field') && !get_field('hide_display_ads'))
{
echo '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-[your-adsense-id]" crossorigin="anonymous"></script>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment