Adsense without plugin in Wordpress

wordpress adsense without plugin

You still did not find a good Adsense plugin for Wordpress? Read this article and do it yourself! Adapt your theme for Adsense injected gently in your posts, widget and in your post index. The result can you see on this site. I don’t use any Adsense plugin anymore. [adsense]

The advantage of this approach is:

  1. Full control over the place and position of the ads.
  2. Automatic insertion of the ad in your content is done gently, without putting it between a bullet list, heading etc. If you still don’t like the way it is done automatically, you can override it with the shortcode [adsense]. Auto insertion is than disabled!
  3. In this way it is also possible to put more ads in a long post, by using the [adsense] shortcode
  4. If the ad can not be posted in a gentle way, it is inserted at the end of the post.
  5. The adsense script is placed at one place and not everywhere in your theme.

Ok, let’s start!

The Adsense code functions

All the routines to display the Adsense ads are using the following routines.

Open functions.php of your theme and add this 2 routines. Make sure that you put your own Adsense values in here. This will be provided in your Adsense dashboard:

function showad_sidebar() {
   return '<script type="text/javascript"><!--
   google_ad_client = "pub-1481813299057084"; google_alternate_color = "FFFFFF";
   google_ad_width = 160; google_ad_height = 600;
   google_ad_channel ="8349928896"; google_color_border = "343434";
   google_color_link = "FFFFFF"; google_color_bg = "343434";
   google_color_text = "333333"; google_color_url = "FFFFFF"; //-->
   <script type="text/javascript"

function showad_content() {
   return '<p><script type="text/javascript"><!--
   google_ad_client = "pub-1481813299057084"; google_alternate_color = "FFFFFF";
   google_ad_width = 468; google_ad_height = 60;
   google_ad_channel ="8349928896"; google_color_border = "FFFFFF";
   google_color_link = "333333"; google_color_bg = "FFFFFF";
   google_color_text = "444444"; google_color_url = "C55964"; //-->
   <script type="text/javascript"

Adsense with a shortcode in your post

With the following piece of code it will be possible to put an adsense ad in your post. This can be done by placing the shortcode [adsense] in your post.

/* Add the following tag for displaying manualy the ads */
add_shortcode('adsense', 'showad_content');

Adsense as a widget

To create your own Adsense widget, add the following code to functions.php of your theme:

 * Adsense Widget Class
class adsense_widget extends WP_Widget {

   /* constructor -- name this the same as the class above */
   function adsense_widget() {
      parent::WP_Widget(false, $name = 'My Adsense Widget'); 

   /* @see WP_Widget::widget -- do not rename this */
   function widget($args, $instance) { 
      extract( $args );
      $title = apply_filters('widget_title', $instance['title']);
      <?php echo $before_widget; ?>
      <?php if ( $title )
      echo $before_title . $title . $after_title; 
      $ad = showad_sidebar();
      echo '<div class="adsense_sidebar">' . $ad . '</div>' ?>
      <?php echo $after_widget; ?>

   /* @see WP_Widget::update -- do not rename this
    * Function to update the values (title) in the widget menu
   function update($new_instance, $old_instance) { 
      $instance = $old_instance;
      $instance['title'] = strip_tags($new_instance['title']);
      return $instance;

   /* @see WP_Widget::form -- do not rename this 
    * Function to fill in the values (title) in the widget menu
   function form($instance) { 
      $title = esc_attr($instance['title']);
      <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
      <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('      title'); ?>" type="text" value="<?php echo $title; ?>" />
} // end class adsense_widget
add_action('widgets_init', create_function('', 'return register_widget("adsense_widget");'));

After saving the functions.php file, the widget “My Adsense Widget” will be available in your widget menu. In this widget it is possible to put there your own title for the widget.

Adsense between your post in the index

In case you want to display ads on your frontpage between your posts, you need to add the following code parts into your index.php.

First search in index.php to the part  ”if (have_posts())” and insert the following 2 variables (bold) next to it, like in this example:

<?php if (have_posts()) {
   $postnum = 1; 
   $showadsense = 1;

After that you need to find the part ”while (have_posts())” and add the piece of code where you want the ad placed. The $showadsense variable contains after which post your ad will be inserted between your posts.

   <?php while (have_posts()) : the_post(); ?>
   <?php if ($postnum == $showadsense) { 
      echo showad_content();

Adsense inserted in your content

Right now you’re able to put the Adsense ads in your content by using the [adsense] shortcode. But it is a lot of work to put this manually in all your old posts. In this part we will insert Adsense automatically in a gentle way, without disturbing the layout. You always will be able to correct the automatic way by putting the [adsense] shortcode in your post, auto insertion of ads will be disabled.

Add the next piece of code into your functions.php file of your theme:

function inject_adsense_gently($content) {
   if (is_page()) {
      return $content; // Do not put an ad on a page
   // Check if a manual position of the ad with shortcode [adsense] exists
   if(STRPOS($content, "adsense")) {
      return $content; // Already an ad on this page
   } else {
      $contentArray = explode("</p>", $content); // All content in an array, separated on </p>
      $content = "";
      $adInserted = false;
      $adAfterParagraph= 0; //show ad after paragraph nr
      //$adAfterParagraph = round(count($contentArray) / 2); //show ad after paragraph nr

      for ($i = 0; $i <count($contentArray); $i++) {
         $content = $content . $contentArray[$i];
         if ($i == $adAfterParagraph) {
            // Check if the next paragraph is a bullet or numbered list
            if ((($i+1) < count($contentArray)) && (STRRPOS($contentArray[$i+1], '<ol>') >= 1 ) || (STRRPOS($contentArray[$i+1], '<ul>') >= 1 )) {
               // Bullet or numbered list found, take the next paragraph
            } else {
               // Nice place, insert the advertisement
               $content = $content . showad_content();
               $adInserted = true;
         $content = $content . "</p>"; // Put the end marker of each paragraph back
      if (!$adInserted) {
         // The ad was not inserted yet, insert it now at the end of the content
         $content = "<p>" . $content . showad_content(). "</p>";
   return $content;
add_filter('the_content', 'inject_adsense_gently');

The variable $adAfterParagraph contains the position of the ad in your post. Uncomment the next line, to put your ad in the middle of your post:

$adAfterParagraph = count($contentArray) / 2;

In case you want to display the ads also on pages, remove the if statement with the is_page part.

More information about this topic