How to get the most out of Google AdSense from WordPress Blog

Table of Contents

Monetizing your blog is not that important but a fairly significant source of motivation. Do you blog alone or with other authors whom you have gathered? Even a few dollars a month can change your (and their) attitude to the blog.

 

Since Google AdSense is one of the easiest and most popular ways to monetize your blog, we will look at how to use a WordPress blog with multiple authors (although this tutorial also works for single bloggers). We will look at how to set the fields in the profile for the author’s AdSense ads, add this ad using a short code, and let the widget function automatically.

 

Warm-up: Installation of profile fields for author advertisement

 

Google accepts advertisements from multiple AdSense accounts on the same website unless you display advertisements from different versions on the same page. So we will display ads on the authors’ pages of their entries and express our ads on other pages.

 

We can take from each author of the blog it blocks AdSense code and can paste it into the code manually, but it is better to ask them to provide the code and paste it dynamically. Plus, you do not have to do anything manually. The code below lets blog authors update their profiles and insert their AdSense ads.

 

<?php
// show the textarea fields for authors' to enter their AdSense ad codes
// source: https://www.stemlegal.com/greenhouse/2012/adding-custom-fields-to-user-profiles-in-wordpress/
function wptuts_profile_adsense_show( $user ) {
    echo '<h3>Your Google AdSense Ads</h3>
    <table class="form-table">
        <tr>
            <th><label for="adsense_300x250">AdSense Ad Code (300x250)</label></th>
            <td><textarea name="adsense_300x250" id="adsense_300x250" rows="5" cols="30">' . get_user_meta( $user->ID, 'adsense_300x250', true) . '</textarea><br>
            <span class="adsense_300x250">Your Google AdSense JavaScript code for the 300x250 ad space.</span></td>
        </tr>
        <tr>
            <th><label for="adsense_468x60">AdSense Ad Code (468x60)</label></th>
            <td><textarea name="adsense_468x60" id="adsense_468x60" rows="5" cols="30">' . get_user_meta( $user->ID, 'adsense_468x60', true) . '</textarea><br>
            <span class="adsense_468x60">Your Google AdSense JavaScript code for the 468x60 ad space.</span></td>
        </tr>
    </table>';
}
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 propagate the table line and add more types of advertisements if you want. Just do not forget to change the name and other parameters.

 

Create a basic function

 

Now we come to creating our core functions, which other processes will use. Here is the code:

 

<?php
// 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' => '<img src="https://dummyimage.com/300x250" />',
        '468x60'  => '<img src="https://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 '<div class="adsense_' . $ad_type . '">' . $ad_code . '</div>';
    } else {
        return false;
    }
}
?>

Do you see what we have to do?

 

  1. First, we define some codes by default advertisement to be displayed on the pages without records.
  2. Next, we check whether the page is a page record or not.
  3. If this page is a record, we take out the advertising code of the author’s profile and assign it to a variable $ ad_code. Please note that we also use the parameter $ ad_type.
  4. If this is not page one record, we default assign the variable $ ad_code in our code.
  5. If the variable $ ad_code is not empty, we return the ad code in the frame of a div (otherwise, return to false).

 

Done! You can use this anywhere on the subject – both inside and outside the loop.

 

I like it when everything is so simple!

 

Note: Google does not allow to add advertisements from different accounts on the same page. Because of the primary function (and hence other functions), it will not display the default code if the author is not provided in the profile of their principles. If we do this, we will most likely be banned from Google AdSense.

 

Create a shortcode

 

If you want to give authors the freedom to add their advertising anywhere in the record, you can use the shortcode such a plan:

<?php
// 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 more accessible than the primary function: It takes a parameter ad_type, transmits it to our primary function, and returns the result.

 

If you do not want to use parameters and return to the primary function, you do not need the code shown above. Just add this line after the prior process:

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

The only option is our primary function with a default value (in this example, ‘ 468×60 ‘) so that the shortcode will display only such advertising.

 

Automatic ad insertion after “n” the paragraph

 

Perhaps if you do not want to give your authors the freedom of advertising, you can choose to paste your ad automatically. For example, you need it after the first paragraph of each entry. That’s the function:

 

<?php
// the function to insert the ads automatically after the "n" th paragraph in a post
// the following code is borrowed and then edited:
// https://www.internoetics.com/2010/02/08/adsense-code-within-content/
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' );
?>

Create an AdSense widget

 

Creating widgets may seem complicated, but it can make them simple. In our case, we simply conclude our core functions and allow the administrator of your blog to set the default option for him:

 

<?php
// 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';
        }
        ?>
        <p>
        <label for="<?php echo $this->get_field_id( 'ad_type' ); ?>">Ad Type</label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'ad_type' ); ?>" name="<?php echo $this->get_field_name( 'ad_type' ); ?>" type="text" value="<?php echo esc_attr( $ad_type ); ?>" />
        </p>
        <?php
    }
    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 is also straightforward to function: if it is a single record – it displays an advertising poster – if not, it shows a standard advertisement.

 

Conclusion

 

If you have a blog with multiple authors who are not paid for their work, these things can motivate them to write more often. They may even longer advertise and share your articles. In the end, the pages will display their advertising. It’s intelligent and reasonable.

 

Are there any other ideas to monetize a blog with multiple authors? Share in the comments!

 

Fully Managed WordPress Hosting

Nestify’s AWS powered dedicated CPU servers keep your sites fast, secure, and always up to date.

Want faster WordPress?

WordPress Speed Optimization

Try our AWS powered WordPress hosting for free and see the difference for yourself.

No Credit Card Required.

Whitelabel Web Hosting Portal Demo

Launching WordPress on AWS takes just one minute with Nestify.

Launching WooCommerce on AWS takes just one minute with Nestify.