2013年2月26日星期二

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:

// show the textarea fields for authors' to enter their AdSense ad codes
function wptuts_profile_adsense_show( $user ) {
        echo '

Your Google AdSense Ads

       
               
                       
                       
                        Your Google AdSense JavaScript code for the 300x250 ad space.
               
               
                       
                       
                        Your Google AdSense JavaScript code for the 468x60 ad space.
               
       
';
}
add_action( 'show_user_profile', 'wptuts_profile_adsense_show' );
add_action( 'edit_user_profile', 'wptuts_profile_adsense_show' );

// save the changes above
function wptuts_profile_adsense_save( $user_id ) {
        if ( ! current_user_can( 'edit_user', $user_id ) )
                return false;
        update_user_meta( $user_id, 'adsense_300x250', $_POST['adsense_300x250'] );
        update_user_meta( $user_id, 'adsense_468x60',  $_POST['adsense_468x60']  );
}
add_action( 'personal_options_update', 'wptuts_profile_adsense_save' );
add_action( 'edit_user_profile_update', 'wptuts_profile_adsense_save' );
?>

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:

// our main function to return the ad codes
// remember: other functions below use this function, too!
function wptuts_return_adsense( $ad_type = '468x60' ) {
        // the default ad codes - don't forget to change them!
        $default_ad_codes = array(
                '300x250' => 'http://dummyimage.com/300x250
" />',
                '468x60'  => 'http://dummyimage.com/480x60
" />'
        );
        if ( is_single() ) {
                global $post;
                $user_id = $post->post_author;
                $ad_code = get_user_meta( $user_id, 'adsense_' . $ad_type, true );
        } else {
                $ad_code = $default_ad_codes[$ad_type];
        }
        if ( $ad_code != '' ) {
                // we'll return the ad code within a div which has a class for the ad type, just in case
                return '
' . $ad_code . '
';
        } else {
                return false;
        }
}
?>

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:

// shortcode for the above function
// usage: [display_adsense ad_type="468x60"]
function display_adsense_sc( $atts ) {
        extract( shortcode_atts( array(
                'ad_type' => '468x60'
        ), $atts ) );
        return wptuts_return_adsense( $ad_type );
}
add_shortcode( 'display_adsense', 'display_adsense_sc' );
?>

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:

// usage: [display_adsense]
add_shortcode( 'display_adsense', 'wptuts_return_adsense' );
?>

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:

// the function to insert the ads automatically after the "n"th paragraph in a post
// the following code is borrowed from Internoetics, then edited:
function wptuts_auto_insert_adsense( $post_content ) {
        if ( !is_single() ) return $post_content;
        $afterParagraph = 1; // display after the "n"th paragraph
        $adsense = wptuts_return_adsense( '468x60' );
        preg_match_all( '/<\/p>/', $post_content, $matches, PREG_OFFSET_CAPTURE );
        $insert_at = $matches[0][$afterParagraph - 1][1];
        return substr( $post_content, 0, $insert_at) . $adsense . substr( $post_content, $insert_at, strlen( $post_content ) );
}
add_filter( 'the_content', 'wptuts_auto_insert_adsense' );
?>

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 to display the ads on the sidebar
class Wptuts_AdSense_Widget extends WP_Widget {
        public function __construct() {
                parent::__construct(
                        'wptuts_adsense_widget', // base ID of the widget
                        'Wptuts+ AdSense Widget', // the name of the widget
                        array( 'description' => 'Wptuts+ AdSense Widget Settings' ) // the description for the widget
                );
        }

        public function form( $instance ) {
                if ( isset( $instance[ 'ad_type' ] ) ) {
                        $ad_type = $instance[ 'ad_type' ];
                } else {
                        $ad_type = '300x250';
                }
                ?>
               
                 
               
               
               
        }

        public function update( $new_instance, $old_instance ) {
                $instance = array();
                $instance[ 'ad_type' ] = strip_tags( $new_instance[ 'ad_type' ] );
                return $instance;
        }

        public function widget( $args, $instance ) {
                echo wptuts_return_adsense( $instance[ 'ad_type' ] );
        }

}

function myplugin_register_widgets() {
        register_widget( 'Wptuts_AdSense_Widget' );
}

add_action( 'widgets_init', 'myplugin_register_widgets' );
?>

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!


没有评论:

发表评论