How to Share Adsense Revenue With Your Authors

How to Share Adsense Revenue With Your Authors

Tutorial Details
  • Program: WordPress
  • Version: 1.0
  • Difficulty: Intermendiate

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.

// Create Custom Settings Menu
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

function adshare_menu() {
	//Create Sub-Level Menu Page under Settings
	add_submenu_page( 'options-general.php', 'Ad Share Settings', 'Ad Share', 'manage_options', 'adshare_settings_page', 'adshare_settings_page');
}

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.

function adshare_settings_page() {
	// Must check that the user has the required capability
	if (!current_user_can('manage_options'))
	{
		wp_die( __('You do not have sufficient permissions to access this page.') );
	}
?>
<div class="wrap">
		<h2>Ad Share Settings</h2>
		<form method="post" action="options.php">
			<?php wp_nonce_field('update-options') ?>
			<p><strong>Adsense Publisher ID:</strong><br />
				<input type="text" name="publisher-id" size="45" value="<?php echo get_option('publisher-id'); ?>" />
			</p>
			<p><input type="submit" name="Submit" value="Save" /></p>
			<input type="hidden" name="action" value="update" />
			<input type="hidden" name="page_options" value="publisher-id" />
		</form>
	</div>
<?php
}

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.

add_action( 'show_user_profile', 'adshare_profile_fields' );
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.

function adshare_profile_fields( $user ) { ?>
	<h3>Extra Field</h3>
	<table class="form-table">
		<tr>
			<th><label for="twitter">Adsense Publisher ID</label></th>
			<td>
				<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 />
				<span class="description">Add your Publisher ID</span>
			</td>
		</tr>
	</table>
<?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.

add_action( 'personal_options_update', 'adshare_save_profile_fields' );
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.

function adshare_save_profile_fields( $user_id ) {
	if ( !current_user_can( 'edit_user', $user_id ) ){
		return false;
	}
	update_usermeta( $user_id, 'publisher-id', $_POST['publisher-id'] );
}

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

function adsense_ad() {
	if(get_the_author_meta( 'publisher-id' )){
		$input = array(get_option('publisher-id'), get_the_author_meta( 'publisher-id' ));
	}else{
		$input = array(get_option('publisher-id'));
	}
	shuffle($input);
	?>
	<script type="text/javascript"><!--
	google_ad_client = "ca-<?php echo $input[0]; ?>";
	google_ad_width = 468;
	google_ad_height = 60;
	//-->
	</script>
	<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
	<?php
}

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.

if(get_the_author_meta( 'publisher-id' )){
	$input = array(get_option('publisher-id'), get_the_author_meta( 'publisher-id' ));
}else{
	$input = array(get_option('publisher-id'));
}

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.

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.

<script type="text/javascript"><!--
google_ad_client = "ca-<?php echo $input[0]; ?>";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<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.

    <?php get_template_part( 'content', 'single' ); ?>
    <?php adsense_ad(); ?>  //Call Adsense Function
    <?php comments_template( '', true ); ?>

Total Code

Here’s the entire chunk of code from our tutorial. Hope you find it useful.

// Create Custom Settings Menu
add_action('admin_menu', 'adshare_menu');

function adshare_menu() {
	//Create Sub-Level Menu Page under Settings
	add_submenu_page( 'options-general.php', 'Ad Share Settings', 'Ad Share', 'manage_options', 'adshare_settings_page', 'adshare_settings_page');
}

function adshare_settings_page() {
	//must check that the user has the required capability
	if (!current_user_can('manage_options'))
	{
		wp_die( __('You do not have sufficient permissions to access this page.') );
	}
?>
<div class="wrap">
		<h2>Ad Share Settings</h2>
		<form method="post" action="options.php">
			<?php wp_nonce_field('update-options') ?>
			<p><strong>Adsense Publisher ID:</strong><br />
				<input type="text" name="publisher-id" size="45" value="<?php echo get_option('publisher-id'); ?>" />
			</p>
			<p><input type="submit" name="Submit" value="Save" /></p>
			<input type="hidden" name="action" value="update" />
			<input type="hidden" name="page_options" value="publisher-id" />
		</form>
	</div>
<?php
}

add_action( 'show_user_profile', 'adshare_profile_fields' );
add_action( 'edit_user_profile', 'adshare_profile_fields' );

function adshare_profile_fields( $user ) { ?>
	<h3>Extra Field</h3>
	<table class="form-table">
		<tr>
			<th><label for="twitter">Adsense Publisher ID</label></th>
			<td>
				<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 />
				<span class="description">Add your Publisher ID</span>
			</td>
		</tr>
	</table>
<?php }

add_action( 'personal_options_update', 'adshare_save_profile_fields' );
add_action( 'edit_user_profile_update', 'adshare_save_profile_fields' );

function adshare_save_profile_fields( $user_id ) {
	if ( !current_user_can( 'edit_user', $user_id ) ){
		return false;
	}
	update_usermeta( $user_id, 'publisher-id', $_POST['publisher-id'] ); //
}

function adsense_ad() { 
    if(get_the_author_meta( 'publisher-id' )){
        $input = array(get_option('publisher-id'), get_the_author_meta( 'publisher-id' ));
    }else{
        $input = array(get_option('publisher-id'));
    }
    shuffle($input);
?>

<script type="text/javascript">
	<!--
	google_ad_client = "ca-<?php echo $input[0]; ?>";
	google_ad_width = 468;
	google_ad_height = 60;
	//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<?php
}

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!

Kailan Wyatt is b4ucode on Codecanyon
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Pingback: How to Share Adsense Revenue With Your Authors | Qtiva

  • http://www.innovativephp.com/tutorials Rakhitha Nimesh

    Excellent method to reward writers and get free posts to your site. Also the facebook option will be pretty good as well. I am going to use this to allow my authors to share their facebook page for users to like.

    Thanks for sharing

  • http://webdesignergeeks.com Ajay Patel

    Really ultimate solution.
    Help lot to Multi author blogger.

    Thanks for sharing !!! :)

  • http://www.b4ucode.com Kailan Wyatt
    Author

    Thanks so much for the comments. I am looking forward to creating more articles for everyone’s benefit. I find the possibilities of the sharing system to be endless, one can share other items like:

    - Amazon Items
    - Ebay Items
    - Banners / Affiliate Ads
    - Facebook, Twitter, Google + Follow widgets

    And so much more. Let me know what ideas you can come up with.

    • http://facebookcoverlayout.com Menj

      I’m not sure if this is allowed in adsense TOS. :)
      One thing I’m sure that we are not allowed to customize the generated ads code.

      TY

      • http://wordpressdeveloper.me Muhammad Adnan

        @Menj @Kailan

        Can you guys confirm It is possible or not regarding Adsense TOS ??

  • http://tusfrasesypoemas.com/ Andres poemas

    I was looking for info to use google adsense to my web

  • Connor Crosby

    Very unique and helpful tutorial! I agree that it’s a great way to reward authors who write for free.

  • Pingback: WordPress Tip: How to Share Adsense Revenue With Your Authors - Notagrouch.com

  • Pingback: Mobile Site Builder, WP Plugin Blacklist, Google Penguin, Around The Web

  • Pingback: How to Share Adsense Revenue With Your Authors | Wptuts+ | How to Make Money with Pay Per Click

  • Pingback: Mobile Site Builder, WP Plugin Blacklist, Google Penguin, Speedlink 19:2012 | Blog Makegrn 2.0

  • http://www.tweaktag.com tweak tag

    Fantastic Tutorial, Spending and dedicating to make a super result, Instead of trying plugin, It seems great for WordPress,.. :D

  • Pingback: Tinymce aanpassen? - Internet Miljonair Forum - Alles over Internet Marketing

  • Pingback: My Tuts on Envato’s WP Tutsplus « B4ucode Blog

  • Pingback: How to make a Squidoo/Hubpages clone in Wordpress | B4ucode Blog

  • http://v7web.com v7web

    Brilliant! Many thanks Kailan,

    I have only just found tutsplus but it was just what I had been searching for.
    Loads of great tutorials for building WordPress plugins and especially for me as I am currently setting up a WordPress Multisite network.

    Thanks

  • Peter

    I actually copy and paste your code with just a change, the ad size.

    What I see as a result is that a lot of posts have the ad with no pub ID, I see this in the code:

    1- The ad is actually showing, but there is no ID, who wins there ?
    2- Can you please share why this happens and how to fix it ?
    3- If there are editors with no ID at all what will happen?

    I am looking in a way to display always my ads and share with the users that actually have an ID in their profile, for now what I did was filling my ID in all the editors profiles.

    4- The ID should be entered like this: “pub-789347894379834″ ?
    5- The percentage share is 50% ?

    Thanks for sharing, very good article.

  • Peter

    It seems your code tags for comments are not working, will try again here, what I see is this:

    <script type=”text/javascript”>
    <!–
    google_ad_client = “ca-”;
    google_ad_width = 336;
    google_ad_height = 280;
    //–>
    </script>

    Thanks.

  • http://www.cryptlife.com/ Arjun

    Does Adsense allows to use more than 1 account in a site?

    • dadeecc

      Yes, they allow to use as much accounts as you’d like on a single website but, you can only use a single PUB code per page. Which is very good imo, you write you’re article and all the ads are yours, and I do the same with mine, on the same website. Hope this was helpful.

  • Pingback: WordPress Tip: How to Share Adsense Revenue With Your Authors | Oscar González

  • mmn

    Seems as lot like similar pugin/modules seen for joomla… never got that setup though…seems a bit easier in wp

  • Shelob9

    When I tried this in WP 3.5 it initially would not save the publisher ID in the user profile. Turns out that update_usermeta is deprecated. On line 48 I changed update_usermeta to update_user_meta and it worked as it should.

  • xpl0iter

    I just used this tutorial to make a plugin for wordpress: Check this out: http://wordpress.org/extend/plugins/revenue-share-plugin-for-authorsrsp/