How to Create an Ad Space without Any Plugin

If you own a blog that is useful for the people, you can quickly make your efforts fruitful by” Blog Monetization.” It is a process to create some extra custom space on your blog to showing related ad’s from various sources.

I know that Google Adsense is one of the most popular and influential sources for blog Monetizing and it has no cost at all, but still many people failed to achieve an approved Google Adsense account. Sometimes, Google doesn’t accept an excellent blog for ad space issues, so it needs to fix up before applying for Google Adsense.

There are a lot of WordPress plugins are available to create ad space, but in this tutorial, I will show you how to create ad space without any plugin.

You can do it by two ways:

  1. Download and install Google Adsense Plug-in.
  2. Add Some Function to your functions.php file.

The first process is too easy but the second process is a bit tricky. Here I am going to show the step by step process to do it.

Step-1. Open your function.php file and add the following codes:

<?php
function profile_adsense_show( $user ) {
    echo '<h3>Google AdSense codes</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">Google AdSense JavaScript code for the 468x60 ad space.</span></td>
        </tr>
    </table>';
}

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


// Save the changes above
function 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', 'profile_adsense_save' );
add_action( 'edit_user_profile_update', 'profile_adsense_save' );
?>

After add this code you will see two custom fields in your WordPress profile like the picture below. You can put your Google Adsense code into these fields and update profile, so that your Adsense code is saved with your profile.

But the process is not finished yet.

Step-2 Now you have to add main function to your functions.php that is run the process. Just add following piece of code at the end of your function:

-<?php
// the main function is used to return the ad codes
// remember that the other functions below also use this function, too!
function 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;
    }
}
?>

The process is now complete, but you can also add more functionality to it, like shortcode, automated ad display and custom widget for Google Adsense.

To add shortcode use this code snippets:

Step-3: If you want to put your Google Adsense code anywhere in your post, you can use wp short-code functions for this purpose. Simply add the following code to add shortcode functionality now:

<?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 return_adsense( $ad_type );
}
add_shortcode( 'display_adsense', 'display_adsense_sc' );
<?
Or you can add this single line code to make the process straightforward:
<?php
// usage: [display_adsense]
add_shortcode( 'display_adsense', 'return_adsense' );
?>

For automated Display:

Step-4: If you want an automated system that represents the ads after the first paragraph of every post this code might be helpful for you. By this code, you do not need to insert any shortcode manually. The ads will automatically show in the first paragraph of every post.

<?php
// the function to insert the ads automatically after "n" paragraph in a post
function auto_insert_adsense( $post_content ) {
    if ( !is_single() ) return $post_content;
    $afterParagraph = 1; // display after the "n"th paragraph
    $adsense = 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', 'auto_insert_adsense' );
?>

For Add custom widget:

Now we can go to build our Google Adsense widget that would be enable to display ads in the sidebar. The following codes can obviously does this trick:

<?php
// the widget to display the ads on the sidebar
class AdSense_Widget extends WP_Widget {
    public function __construct() {
        parent::__construct(
            'adsense_widget', // base ID of the widget
            'AdSense Widget', // the name of the widget
            array( 'description' => '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 return_adsense( $instance[ 'ad_type' ] );
    }
}

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

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

 

Now you will see the change in your widget menu.

Note: Don’t add beginning and end tags of .php code if you add the code to your function.php. You can also make a custom plug-in by this code. If you still don’t understand anything, please feel free to notify us by the comment, and we will try our best to clear the matter.