Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Automatically Switch between two AdSense accounts based on the author post

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.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="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></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. 🙂

Related Posts:

  1. How to Add Author Editing Capability to Existing Post by Admin
  2. Different posttypes for different authors
  3. How to check if a user (not current user) is logged in?
  4. How to remove the author pages?
  5. Select subscriber as author of post in admin panel?
  6. Echo author ID in author.php
  7. Woocommerce: assign an “author” to a product
  8. How can HTML be allowed in Author Bio?
  9. Programatically change post author
  10. Restrict one post per author
  11. How to get author’s name by author’s id
  12. Author page: Comments and Ratings?
  13. Give Author users the right to embed
  14. How to get post author name by its ID inside a function?
  15. How can I publish a post with two or more authors?
  16. If Else Gravatar Author Picture
  17. Displaying custom post types in author.php
  18. Add rewrite endpoint to author page + pagination
  19. Order the users by the date of their latest post
  20. What’s a good way to put authors into groups
  21. Permalink to user profile
  22. Show Author in custom rss feed
  23. What is the difference between user_description and description?
  24. How can an author be subscribed to the posts of another author and receive notifications of the new posts?
  25. No authors in change author dropdown
  26. Show author archive pages for authors with no posts
  27. More posts from the current author
  28. List authors of site with link and gravatar
  29. How we can get the author ID by its Name
  30. How to hide posts count and posts of other users from edit.php for contributors and authors
  31. User Published Post Count
  32. Custom size for image uploaded to custom field in user profile?
  33. What capability allows someone to change a page’s/post’s author?
  34. How to get posts without author?
  35. Creating distinct pages for author profiles and lists of posts by author
  36. Allow Content Author to Publish, But Not Edit or Delete
  37. Just wanted to Pull Author’s Link
  38. Making WordPress author follow button, need missing ingredient
  39. list author’s posts in author.php
  40. Get only the author profile picture image url inside a loop
  41. Get the comment author ID by the comment ID
  42. Author profile visitors in last 7 days in front end without plugins
  43. How To Remove The Author(s) From Certain Posts
  44. Is it possible to set/override default the_author_posts_link to login name?
  45. add capability to author role to be able to delete attachments
  46. Remove /author/ from the author profile url
  47. Google Authorship on a multi-author blog?
  48. Replace Gravatar with img URL for avatars
  49. Change author base slug to user role
  50. Get the excerpt for a post created by the related author
  51. Custom Author Fields + Existing Taxonomy – Integrating the Two Dynamically?
  52. Display Author role in archive or author page
  53. WordPress Author Page
  54. get the username of a user in his author page
  55. Editing the loop for Author pages
  56. What is wrong with this code?
  57. Limit REST API output to current logged in user that is also author of the content
  58. Display only entire most recent post on author page?
  59. I only want to Display Author Link in authors bio
  60. How can I add “posted by ‘author'” to each post?
  61. Is it possible to get_terms by author?
  62. no emails for a user account
  63. Author list ‘special’ author first then ‘normal’ author
  64. hide something in single page if current author has only one post
  65. Show selected value in a drop down menu
  66. Can I stop authors from changing the font in their post?
  67. Author page doesn’t change after username change [duplicate]
  68. Custom Author Loop
  69. Output author and description if description is not empty
  70. get_the_author_meta( $field, 0 ) returns the administrator $field
  71. WP authors page
  72. Send transactional email: first user’s post
  73. Showing author’s page with no posts
  74. Show woocommerce products in author archive
  75. Author bio Social Links
  76. Plugin for an author bio popup box?
  77. How to link avatar and nickname to profile
  78. How to link to the current User/Author Profile page?
  79. Author website URL
  80. Author custom fields post meta, the code?
  81. How to update author display name on blog posts based on user role
  82. How can I get the author description’s excerpt?
  83. Change destination of author link to BuddyPress member profile
  84. Filter Author Bio
  85. How to remove the dropdown author data from the post edit page
  86. Conditional/Check: IF comment author is the post author
  87. Get last revision author, author-link and date
  88. How to use Buddypress xProfile field as WordPress Bio
  89. If post author ID is..show that
  90. What do you put in child themes author tag?
  91. Make user as Default contributor for all Blog Posts
  92. Multiple Unique Author Profiles
  93. Getting $curauth->ID to work inside a shortcode
  94. How to list the authors of all revisions?
  95. WordPress doesn’t show all posts of a specific author
  96. How to add child page to author.php
  97. Link Author’s Website in Author Bio
  98. Custom avatar image link added to the profile page
  99. Remove admin role from the “Author” user listing on individual post screen
  100. Post list based on the page author
Categories author Tags account, adsense, author
Custom failed login error messages for users based on user role?
remove duplicate name submenu link from the custom post type

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress