: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Output auto ads enabled code in head
* @var bool $privacy_enabled Whether to wait for user consent.
* @var bool $npa_enabled Whether to show non-personalized ads.
* @var string $client_id The Google AdSense client ID.
* @var bool $top_anchor AdSense anchor ad on top of pages.
* @var string $top_anchor_code The code for top anchor ads.
* @var string $script_src AdSense script url.
* @var bool $add_publisher_id Whether to add the publisher ID to the AdSense JavaScript URL.
if ( $privacy_enabled ) : ?>
document.addEventListener('advanced_ads_privacy', function (event) {
(event.detail.state !== 'accepted' && event.detail.state !== 'not_needed' && !advads.privacy.is_adsense_npa_enabled())
// google adsense script can only be added once.
var script = document.createElement('script'),
first = document.getElementsByTagName('script')[0];
script.crossOrigin = 'anonymous';
script.src = '<?php echo esc_url( $script_src ); ?>';
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- relevant user input has already been escaped.
} elseif ( ! $add_publisher_id ) {
printf( 'script.dataset.adClient = "%s";', esc_attr( $client_id ) );
first.parentNode.insertBefore(script, first);
// phpcs:disable WordPress.WP.EnqueuedResources
'<script async src="%s"></script><script>%s</script>',
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- the snippet has already been escaped.
// Don't add the data-ad-client attribute when the publisher ID is appended to the script URL.
'<script %s async src="%s" crossorigin="anonymous"></script>',
! $add_publisher_id ? 'data-ad-client="' . esc_attr( $client_id ) . '"' : '',