How to Share Adsense Revenue With Your Authors

This tutorial will demonstrate how you can easily share Adsense Ad space with your authors. It would be quite useful in attracting new authors to blog on your site, in return for some Adsense revenue for what they’ve written.
The tutorial is only a stepping stone to a more feature rich site for your writers and users. Discover how to add extra user fields and how to manipulate them on your site.

Step 1 Creating Settings Page

For this tutorial I am utilising the default theme Twenty Eleven. You can use your current theme and tweak where necessary.
As the first step, we would like to create a page to accept the default Publisher ID. I was fortunate to come across this great and simple tutorial ”
Quick Tip: Create A WordPress Global Options Page“. It’s a good read and I will adopt some methods into this tutorial.
First locate the functions.php file in your currently activated theme. Then at the bottom add the following code snippet. The snippet will register a new Admin Menu, it will call the function adshare_menu.
  1. // Create Custom Settings Menu  
  2. add_action('admin_menu''adshare_menu');  
Next, we create the adshare menu and call the add_submenupage function. The first parameter will determine the parent menu for the Settings Page.
“Here are some other other Parent Menu to choose from”
Submenu Pages
  1. function adshare_menu() {  
  2.     //Create Sub-Level Menu Page under Settings  
  3.     add_submenu_page( 'options-general.php''Ad Share Settings''Ad Share''manage_options''adshare_settings_page''adshare_settings_page');  
  4. }  

Creating the Setting Page Display

Now we will design the layout for the settings page. Note that the function is called adshare_settings_page, just like the last the parameter in our previous code.
  1. function adshare_settings_page() {  
  2.     // Must check that the user has the required capability  
  3.     if (!current_user_can('manage_options'))  
  4.     {  
  5.         wp_die( __('You do not have sufficient permissions to access this page.') );  
  6.     }  
  7. ?>  
  8. <div class="wrap">  
  9.         <h2>Ad Share Settings</h2>  
  10.         <form method="post" action="options.php">  
  11.             <?php wp_nonce_field('update-options') ?>  
  12.             <p><strong>Adsense Publisher ID:</strong><br />  
  13.                 <input type="text" name="publisher-id" size="45" value="<?php echo get_option('publisher-id'); ?>" />  
  14.             </p>  
  15.             <p><input type="submit" name="Submit" value="Save" /></p>  
  16.             <input type="hidden" name="action" value="update" />  
  17.             <input type="hidden" name="page_options" value="publisher-id" />  
  18.         </form>  
  19.     </div>  
  20. <?php  
  21. }  
The result will look like the following:

Step 2 Creating an Extra User Field

Our next step is to create the option for users to save their own Publisher ID

Adding Profile Actions

To add the ability for both admin and users to update a user profile field, we need to call two WP Action Hooks. The hooks are edit_user_profile and show_user_profile. Add the this snippet to your file.
  1. add_action( 'show_user_profile''adshare_profile_fields' );  
  2. add_action( 'edit_user_profile''adshare_profile_fields' );  

Adding the Form Field

Now that you’ve added those hooks, let’s call the function in the second parameter adshare_profile_field. This function holds the form fields that will be displayed in a user’s edit form. You can tweak the HTML any way that you like, but be sure to maintain the correct name and value attributes for this tutorial.
  1. function adshare_profile_fields( $user ) { ?>  
  2.     <h3>Extra Field</h3>  
  3.     <table class="form-table">  
  4.         <tr>  
  5.             <th><label for="twitter">Adsense Publisher ID</label></th>  
  6.             <td>  
  7.                 <input type="text" name="publisher-id" id="publisher-id" value="<?php echo esc_attr( get_the_author_meta( 'publisher-id', $user->ID ) ); ?>" class="regular-text" /><br />  
  8.                 <span class="description">Add your Publisher ID</span>  
  9.             </td>  
  10.         </tr>  
  11.     </table>  
  12. <?php }  

Saving Profile Field

So far, we’ve added the form fields but that does not save them. In order to update a user profile, we need two action hooks; personal_options_update & edit_user_profile_update. Add the following hooks.
  1. add_action( 'personal_options_update''adshare_save_profile_fields' );  
  2. add_action( 'edit_user_profile_update''adshare_save_profile_fields' );  
Now let’s write the adshare_save_profile_fields function. This function will take the POST data and save it to the user meta information. Just like when a user updates their name, our new field will be added.
  1. function adshare_save_profile_fields( $user_id ) {  
  2.     if ( !current_user_can( 'edit_user'$user_id ) ){  
  3.         return false;  
  4.     }  
  5.     update_usermeta( $user_id'publisher-id'$_POST['publisher-id'] );  
  6. }  
There we have it, a fully functioning Extra Field for our authors. In the next step, we’re going to make use of that new field.

Step 3 Adding Adsense to Post

If you’ve made it this far, I am happy for you. We have one last function to create in our functions.php file. Let’s create the function that will choose the publisher ID and display it in the Google Ad on the site
  1. function adsense_ad() {  
  2.     if(get_the_author_meta( 'publisher-id' )){  
  3.         $input = array(get_option('publisher-id'), get_the_author_meta( 'publisher-id' ));  
  4.     }else{  
  5.         $input = array(get_option('publisher-id'));  
  6.     }  
  7.     shuffle($input);  
  8.     ?>  
  9.     <script type="text/javascript"><!--  
  10.     google_ad_client = "ca-<?php echo $input[0]; ?>";  
  11.     google_ad_width = 468;  
  12.     google_ad_height = 60;  
  13.     //-->  
  14.     </script>  
  15.     <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>  
  16.     <?php  
  17. }  
Now for a break down. The first few lines checks to see if the author has a Publisher ID added, if they do not then only the admin Publisher ID will be used.
  1. if(get_the_author_meta( 'publisher-id' )){  
  2.     $input = array(get_option('publisher-id'), get_the_author_meta( 'publisher-id' ));  
  3. }else{  
  4.     $input = array(get_option('publisher-id'));  
  5. }  
The function shuffle, as simple as it is, shuffles the values of the array. This is important to get the Publisher ID to change when a page is visited or refreshed.
  1. shuffle($input);  
The last part of this function, displays the Adsense Script. The Client ID variable is replaced with $input[0], which will show the first value of the shuffled array. Simple but effective.
  1. <script type="text/javascript"><!--  
  2. google_ad_client = "ca-<?php echo $input[0]; ?>";  
  3. google_ad_width = 468;  
  4. google_ad_height = 60;  
  5. //-->  
  6. </script>  
  7. <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>  

Call Function on Page

Finally, we can call the function adsense_ad() in our single.php file. For this tutorial, I called the function between the post and the comments.
  1. <?php get_template_part( 'content''single' ); ?>  
  2. <?php adsense_ad(); ?>  //Call Adsense Function  
  3. <?php comments_template( '', true ); ?>  

Total Code

Here’s the entire chunk of code from our tutorial. Hope you find it useful.
  1. // Create Custom Settings Menu  
  2. add_action('admin_menu''adshare_menu');  
  3.   
  4. function adshare_menu() {  
  5.     //Create Sub-Level Menu Page under Settings  
  6.     add_submenu_page( 'options-general.php''Ad Share Settings''Ad Share''manage_options''adshare_settings_page''adshare_settings_page');  
  7. }  
  8.   
  9. function adshare_settings_page() {  
  10.     //must check that the user has the required capability  
  11.     if (!current_user_can('manage_options'))  
  12.     {  
  13.         wp_die( __('You do not have sufficient permissions to access this page.') );  
  14.     }  
  15. ?>  
  16. <div class="wrap">  
  17.         <h2>Ad Share Settings</h2>  
  18.         <form method="post" action="options.php">  
  19.             <?php wp_nonce_field('update-options') ?>  
  20.             <p><strong>Adsense Publisher ID:</strong><br />  
  21.                 <input type="text" name="publisher-id" size="45" value="<?php echo get_option('publisher-id'); ?>" />  
  22.             </p>  
  23.             <p><input type="submit" name="Submit" value="Save" /></p>  
  24.             <input type="hidden" name="action" value="update" />  
  25.             <input type="hidden" name="page_options" value="publisher-id" />  
  26.         </form>  
  27.     </div>  
  28. <?php  
  29. }  
  30.   
  31. add_action( 'show_user_profile''adshare_profile_fields' );  
  32. add_action( 'edit_user_profile''adshare_profile_fields' );  
  33.   
  34. function adshare_profile_fields( $user ) { ?>  
  35.     <h3>Extra Field</h3>  
  36.     <table class="form-table">  
  37.         <tr>  
  38.             <th><label for="twitter">Adsense Publisher ID</label></th>  
  39.             <td>  
  40.                 <input type="text" name="publisher-id" id="publisher-id" value="<?php echo esc_attr( get_the_author_meta( 'publisher-id', $user->ID ) ); ?>" class="regular-text" /><br />  
  41.                 <span class="description">Add your Publisher ID</span>  
  42.             </td>  
  43.         </tr>  
  44.     </table>  
  45. <?php }  
  46.   
  47. add_action( 'personal_options_update''adshare_save_profile_fields' );  
  48. add_action( 'edit_user_profile_update''adshare_save_profile_fields' );  
  49.   
  50. function adshare_save_profile_fields( $user_id ) {  
  51.     if ( !current_user_can( 'edit_user'$user_id ) ){  
  52.         return false;  
  53.     }  
  54.     update_usermeta( $user_id'publisher-id'$_POST['publisher-id'] ); //  
  55. }  
  56.   
  57. function adsense_ad() {  
  58.     if(get_the_author_meta( 'publisher-id' )){  
  59.         $input = array(get_option('publisher-id'), get_the_author_meta( 'publisher-id' ));  
  60.     }else{  
  61.         $input = array(get_option('publisher-id'));  
  62.     }  
  63.     shuffle($input);  
  64. ?>  
  65.   
  66. <script type="text/javascript">  
  67.     <!--  
  68.     google_ad_client = "ca-<?php echo $input[0]; ?>";  
  69.     google_ad_width = 468;  
  70.     google_ad_height = 60;  
  71.     //-->  
  72. </script>  
  73. <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>  
  74. <?php  
  75. }  

Conclusion

Now you know how to add some extra fields to your user profile and can attract some new writers to your blog. The rest of this tutorial is left to your imagination. You can use these methods to share Facebook Likeboxes or other Ad publishing blocks. Happy Coding!


Article by: Kailan Wyatt

Category

10 Resources for Designers and Developers 11 Big Tech Trends You'll See in 2013 12/12/12 56 Mashable Stories 60 second Video how to keep laptop work well 70th Golden Globes ads adsense Adsense Tips Advertising AdWords Amazon Android AngryBird Apple Apple's 2012 Year in Review Apps Apps to Spice Up Your Sex Life article Barack Obama Benefits of a Job Search Community Bill Clinton Bing Bitcoins Blackberry blogging blogging secret blogging tools book Book Review; Content Marketing for Dummies Building an Email List Business News Business tips Campaign Websites Celebrities Charlie the Unicorn Chrome Content Spoiler With Simple Animation Creative Ways to Use Your Favorite Running App CSS Design Competition for Kids Digital Media Resources dnt do not track download drive traffic Dropbox Tools e-commerce ebay Effects Social Networks email enterprenour blog Entertainment Expired Domain Business Facebook facebook hacker File Management Film Firefox for Andoid Friends Sex App Gadget Games Geeks george bush george w. bush gmail golden globes Google Google + (plus) Google API Google Capture Google Trends Google Wallet Google XPhone Guest Blog Post Guest Posting Guide to Super Bowl 2013 Betting Guide To: WordPress Development With Microsoft WebMatrix gyroscopes for optical image stabilization history Hopper DVR Horde Hot Advertising Trends of 2012 How to Backup Your Skype Chat & Audio Conversations Online With Simkl html Html 5 HTML Tool hulu Hurricane Sandy instagram's new policy internet Internet Marketing Internet users iOs 6 iPad iPhone Kim Dotcom Laptop LES link building Linkedin Malware Marketing predictions 2013 marketing tips Microsoft Microsoft Surface Pro Mobile App Websites Mobile phone Mobile web Mobile web design monetizing blog money online Motorola Music NBA Negative Target Fixation New year's resolution News nostalgia Notebook Obama Online Education Online Payment Online Stores Palestinian Statehood parental apps PC Jobs Photoshop for Retina Displays Pics Pinterest Plugin porn in vine President Obama's Speech at Vigil for Newtown Victims Promoting Yourself Via Print Publisher Radio Station Review Romsey RoundCube. mail. cpanel Russian Plane Crash Video Search engine security seff identity security SEO SEO 2013 Shop For Geeky Gifts Smartphone-Enabled Website social media Social Networks Social Sites for Families Softaculous software Spam SquirrelMail superbowl 2013 Tablet Teaching Tool Tech Technology Television The Most Reputable Company in U.S. Things I Learned About Tech in 2012 tips Tools twitter latest U.S. UN Vote Universe Unlocked iPhone 5 in the U.S. upgrade script US election US president vote us presidents video Video Conferencing App for Mac Video Marketing Videos Watercooler Ways to Stay Creative While Working From Home Waze web Web design trend 2013 web traffic webtools Wedding What Is Pinterest? White House Why You Should Say No To Multitasking Wi-Fi Smart Scale windows 7 Windows 8 Windows 8 Review Windows RT Review Wirelessly Monitors Your Driving with Dash WomenWeb Wordpress Wordpress Plugin Working From Home writing web Xbox Yahoo YouTube Favorite YouTube Updates