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;
		$content .= $temp[$i].'</p>';
			$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 type="text/javascript"
	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.
READ  PhpMyAdmin tutorial for bloggers

Feel free to share your queries and thoughts.

4 comments Add ads between content without plugin

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

    above code with your own ad 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)

  2. Hello,

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

    Dr. Ashok Koparday

Leave a Reply

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