Making the Best of Google AdSense in WordPress

Blog monetization is not a "must", but it's a very important source of motivation. Whether you're blogging alone or along with some authors you gathered, earning even a few bucks a month can change your and/or your authors' approach to your blog.

Since Google AdSense is one of the easiest and most popular ways for blog monetization, we're going to see how to use it with a WordPress blog with multiple authors. (Although, this tutorial will also work for single bloggers.) We'll be covering how to set up profile fields for authors' AdSense ads and how to display those ads with a function, with a widget, with a shortcode and automatically inside posts.


Warming Up: Setting Profile Fields for Authors' Own Ads

Google accepts ads from multiple AdSense publishers in the same website as long as you don't display ads from multiple publishers on the same page. Thus, we're going to display our authors' ads on their posts' pages and display our own ads on other pages.

We could ask every author in our blog for their AdSense code blocks and insert them manually in our code, but asking them to provide the code themselves and inserting them dynamically would be a better idea. (Plus, there's no fun in doing stuff manually.) The code below provides the functionality for our authors to update their profiles to insert their own AdSense ads:

You can duplicate the table rows to add more ad types, if you want. Don't forget to change the names and other parameters of the inputs, though.


Building Our Main Function

Now, we get to the part where we build our main function that will be used both on its own and by other functions. Take a look at the code below:

You see what it does?

  1. First off, we define some "default ad codes" to show other than post pages.
  2. Then we check if the page is a "single post page".
  3. If it is a single post page, we fetch the ad codes from the post author's profile and define it in the $ad_code variable. Here, notice that we're also using the $ad_type parameter of the function.
  4. If it's not a single post page, we're defining the $ad_code variable with the default ad codes.
  5. And if the $ad_code variable is not empty, we return the ad code with a div surrounding it. (Otherwise, we return false.)

Done! You can use this function wherever you want inside your theme now - both inside and outside The Loop.

I love it when it's so simple! :)

Remember: Google strictly prohibits you from displaying ads from multiple publishers on the same page. That's why the main function (thus, other functions) will not display the "default ad codes" if the author didn't provide their own codes in their profile. If we did that, we would most definitely get banned from Google AdSense.

Creating the Shortcode

If you want to give your authors the liberty of adding their own ads anywhere inside their posts, you can use a shortcode like below:

It's even simpler than the main function: It takes the ad_type parameter and passes it through our main function and returns the function.

If you don't want any parameters and just return the main function, you don't even need the code above! Just add this line after our main function:

Our main function's only parameter has a default value ('468x60', in our example), so the shortcode will display that kind of ad only.


Inserting the Ads Automatically After "n"th Paragraph

You may not want to give the liberty to your authors to display ads in some cases. If you decide to insert their ads automatically, say, after the first paragraph of every post; the function below is exactly what you're looking for:

If you're going to download the plugin that we're building now (with the Download button at the beginning of the post), don't forget that the line with the add_filter() function will be commented out. Uncomment it to enable this functionality.


Building the AdSense Widget

Building widgets can seem tricky, but it's actually really easy to make them. In our case, we're just going to echo our main function and allow the admin(s) of your blog to set the default parameter for it:

The widget also has a very simple function: If it's a single post page, display the author's ad and if it's not, display the default ad.


Conclusion

If you're running a blog with more than one author without a capital to pay for their work, these tricks could motivate them to write more often. They would even share and promote their own posts more eagerly - after all, they will have their own ads displayed on the pages. It's both clever and good!

Do you have any ideas about monetization of multi author blogs? Share your comments below!

Tags:

Comments

Related Articles