最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Automatically Switch between two AdSense accounts based on the author post

programmeradmin0浏览0评论

We are two managers for one website , each of us have an AdSense account

Can our ad code display in the articles we write, based on the author slug or id?

I tried to use this code in head section , but it did not work :

<?php if (is_author('author 1');) : ?> <script data-ad-client="ca-pub-ءءءءء1ءءءء" async src=".js"></script>; <?php else : ?> <script data-ad-client="ca-pub-ءءءءء2ءءءء" async src=".js"></script> <?php endif; ?>

any ideas to switch automatically between the accounts, not as a revenue share.

We are two managers for one website , each of us have an AdSense account

Can our ad code display in the articles we write, based on the author slug or id?

I tried to use this code in head section , but it did not work :

<?php if (is_author('author 1');) : ?> <script data-ad-client="ca-pub-ءءءءء1ءءءء" async src="https://pagead2.googlesyndication/pagead/js/adsbygoogle.js"></script>; <?php else : ?> <script data-ad-client="ca-pub-ءءءءء2ءءءء" async src="https://pagead2.googlesyndication/pagead/js/adsbygoogle.js"></script> <?php endif; ?>

any ideas to switch automatically between the accounts, not as a revenue share.

Share Improve this question asked Apr 26, 2020 at 18:05 moustafa atiyamoustafa atiya 1 1
  • You should probably enqueue the scripts properly - and is author 1 a placeholder you put in for the sake of your question or is that the actual code? – Tony Djukic Commented Apr 26, 2020 at 20:03
Add a comment  | 

2 Answers 2

Reset to default 0

So I've broken this up into three parts, it'll allow you a lot more flexibility with adding additional authors if that ever comes up.

enqueue_adsense() adds the Google scripts for adsense.

add_inline_adsense() prints the adsense code to display the ad immediately across the top of the page, so right after the body tag.

function_js_async_attr() is needed to add the async code to the script. You'll have to update the list of scripts to exclude though as I don't know what scripts you have running.

function enqueue_adsense() {
    wp_enqueue_script( 'google-adsense', 'https://pagead2.googlesyndication/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     = '12345678910';//these need to be changed
        $data_ad_slot       = '66677666998';//these need to be changed 
    elseif( $author_id == 2 ) :
        $data_ad_client     = '01987654321';//these need to be changed
        $data_ad_slot       = '66677666998';//these need to be changed
    endif;
    echo '<ins class="adsbygoogle" style="display:inline-block;width728px;height:90px; data-ad-client=' . $data_ad_client . '" data-ad-slot="' . $data_ad_slot . '"></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(); //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 );

I've tested this on a site I'm currently working on and it's worked exactly as designed/intended.

If I was building this for a long term use site, I'd even go so far as to have an adSense ID field on user meta, then instead of running the condition that says if the author is X use adSense ID Y I'd just pull the author's provided adSense ID number and only perform the rest of the functions IF an adSense ID has been provided. I don't really have time to build that out right now though and it's not in the scope of what you asked, but I think that'd future proof your project. :-)

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/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 */
?>
发布评论

评论列表(0)

  1. 暂无评论