I have modified the code to suit my site, as I use Jannah WordPress theme
There is no obvious error, but the ads are not running
<?php
/*
Plugin Name: adsense for author post-
Description: Site specific code changes for my site
*/
/* Start Adding Functions Below this Line */
function enqueue_adsense() {
wp_enqueue_script( 'google-adsense', 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', array(), '', false );
}
add_action( 'wp_enqueue_scripts', 'enqueue_adsense', 2 );
function add_inline_adsense() {
global $post;
$author_id = $post->post_author;
//you'll want to adjust the author IDs in the following conditions to match what you need
if( $author_id == 1 ) :
$data_ad_client = 'ca-pub-1111111';//these need to be changed
$data_ad_slot = '111111111111';//these need to be changed
elseif( $author_id == 5 ) :
$data_ad_client = 'ca-pub-555555';//these need to be changed
$data_ad_slot = '555555555';//these need to be changed
endif;
echo '<ins class="adsbygoogle"
style="display:block"
data-ad-client=". $data_ad_client ."
data-ad-slot=". $data_ad_slot ."
data-ad-format="auto"
data-full-width-responsive="true"></ins>';
echo '
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>';
}
add_action( 'wp_head', 'add_inline_adsense', 3 );
function js_async_attr( $tag ) {
$scripts_to_exclude = array('advertisement.js', 'br-news.js', 'desktop.js' ,
'desktop.min.js', 'ie.js', 'live-search.js' ,'parallax.js', 'scripts.js', 'scripts.min.js' ,
'shortcodes.js', 'single.js', 'single.min.js' ,'sliders.js', 'sliders.min.js', 'velocity.js' ,
'videos-playlist.js', 'viewport-scripts.js', 'breakingNews.js' ,'fitvids.js', 'flexMenu.js', 'ilightbox.dev.js' ,
'lazyload.js', 'live-search.js', 'theia-sticky-sidebar.js' ,'tiesticky.dev.js', 'video.dev.js', 'viewport-checker.js');
//add a list of scripts that need to be excluded from async - 'script1.js', 'script2.js', 'script3.js'
foreach( $scripts_to_exclude as $exclude_script ){
if( true == strpos( $tag, $exclude_script ) ) :
return $tag;
endif;
}
return str_replace( ' src', ' async src', $tag );
}
add_filter( 'script_loader_tag', 'js_async_attr', 102 );
/* Stop Adding Functions Below this Line */
?>