Add ads between content without plugin

Plugins are great way to extend WordPress functionality.But they should be used with care.Having great number of plugins does impact your website performance.As a rule of thumb plugins should only be used for major tasks like search engine optimization and caching or doing some particular tasks which needs considerable effort.

For all other minor tasks it is better not to use plugins.One of such tasks is placing advertisement in between content.Ads between content tend to perform better than other placements.Using plugin for this function adds unnecessary code.

Follow below steps to add ads between content without plugin (this method will work for most of the themes).But in case your theme supports ads between content functionality then you must use that

  • open functions.php file of your theme.This file may be located inside your theme folder or any o its subfolder (most likely in include sub folder).Location of file varies from one theme to another theme.This is the most complicated part of the customization.
  • Scroll down to the end of the file.
  • Copy the code shown below and paste it in the file.
add_filter( 'the_content', 'single_ads', 16 );
function single_ads( $content ){
	if ( ! is_user_logged_in() && is_single()) {
	$temp = explode('</p>', $content);
	$add_after = (int)(count($temp)/2);
	$content = NULL;
	for($i=0;$i<count($temp);$i++){
		$content .= $temp[$i].'</p>';
		if($i+1==$add_after){
			$content .= 
			'<div style="float: left; clear: left; margin: 0px 10px 10px 0px;">'.
			'<script type="text/javascript"><!--
google_ad_client = "ca-pub-0000000000000";
/* above_fold_sidebar */
google_ad_slot = "000000000";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>'.
			'</div>';	
		}
	}
	}
	return $content;
}
  • Now above code will display Google Adsense ads between content.The ad will be aligned left and content will be wrapped around it.I have used dummy adsense ad code.You will have to replace the ad code with your adsense code.Above code will work fine for images,videos and all other content (ad codes from other advertising network as well).
  • You can modify the code according to your requirement.For example to justify the ad code to right you need to replace left in above code to right.To display ads between content in middle you need to replace left with center in above code.
  • The above ad will not be shown to logged in user (I prefer this setting so as to avoid accidental clicks on my own ads).If you want to remove that setting simply delete ! is_user_logged_in() && from above code.
  • Ads will be displayed only on single pages.This is important as some themes now show full post on excerpt page and not including this check will result multiple ads displayed on archive pages (violation of google adsense terms) so keep the is_single check intact.

Feel free to share your queries and thoughts.

Share this post on

2 thoughts on “Add ads between content without plugin

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Ashok Koparday

    Hello,
    Namaste.

    This code is not showing up ad in the content of the page. Will you please guide me?

    Dr. Ashok Koparday

    Reply
    1. Esha Upadhyay

      Can you please post the code you are using.You will have to replace



      above code with your own ad code..in case you have any trouble it would be great if you can post your code here (changing your ad code) ..You need to embed code between tags..or you can post on social site as message (if facing issues posting here)

      Reply