'code that prevent Header code from working on a specific roles in WordPress

i have subscribers in my website , AdSense ads are not supposed to appear for them ... but because of auto ads , AdSense ads are still showing up for them ...

now all i need is a code that prevent AdSense header code from working on a specific role in WordPress.

i find that code but i want to hide AdSense header from specific role not so appear it to a specific role

<?php
if ( current_user_can( 'subscriber' ) || !is_user_logged_in()) {
echo '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXX" crossorigin="anonymous"></script>'; 
} ?>


Solution 1:[1]

Have you considered using the Site Kit plugin by Google to insert your ad code? If so you can use the plugin inserted snippet along with the below function:

add_filter( 'googlesitekit_adsense_tag_blocked' , 'restrict_adsense_snippet' );
function restrict_adsense_snippet() {
 // restrict ads for editors programmatically 
 if ( current_user_can('subscriber'))
 return '__return_true';
} 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1