• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Software and Hardware
  • New and Happening
You are here: Home / Programming and Scripting / Replace Wordpress’ more tag with advertisement code

Replace WordPress’ more tag with advertisement code

April 5, 2008 by Sze Hau Leave a Comment

Sometime we want to insert Google Adsense or other advertisement code into middle of a post in WordPress. Replacing the <!--more--> code with Google Adsense or other advertisement code is one of the way.

To replace the <!--more--> with advertisement code, we start with defining a pseudo code.

  1. At the single post page (normally single.php), intercept the output of the content.
  2. Find the location and the length of <!--more-->.
  3. Replace <!–more–> with the advertisement code or insert the advertisement code after or before the <!--more--> tag.
  4. Output the modified content.

Let’s assume the single post page of your WordPress theme is single.php. Find the line in single.php where the_content() function is called, replace the line with the following code:

	if (ob_start()) {
		the_content();
		$content = ob_get_clean ();
		echo replace_more_tag (
			$content,
			get_post_middle_ads ());
	} else {
		the_content();
	}

Now we need to define two more functions(replace_more_tag and get_post_middle_ads). You can put the functions either at top of the code in single.php or inside functions.php in your wordpress theme. I would suggestion to put the functions in functions.php for easier maintenance.

When calling the the_content() function, WordPress automatically replace the <!--more--> tag with <p><span id="more-id"></span></p> where the id is the post’s id. So we need to take care of this when writing the replace_more_tag function.

replace_more_tag($original, $replace, $include_more_tag = true, $include_after = false)

The function finds the <p><span id="more-id"></span></p> in $original and replace it with $replace. If $include_more_tag is true, the <p><span id="more-id"></span></p> will be included. If $include_after is true, it includes the <p><span id="more-id"></span></p> after the $replace else the other way round. Finally the function returns the replaced text.

get_post_middle_ads ()

The function simply return the text defined in the function.

Here is the code for the two functions above:

function 
replace_more_tag (
	$original,
	$replace,
	$include_more_tag = true,
	$include_after = false
) {
	$start = '<p><span id="more-';
	$end = '"></span></p>';
	
	$startPos = strpos ($original, $start);
	if ($startPos === false) 
		return '<p>'.$replace.'</p>'.$original;
	
	$endPos = strpos ($original, $end, $startPos + strlen($start));
	if ($endPos === false) 
		return '<p>'.$replace.'</p>'.$original;
	
	$moreTag = substr ($original, $startPos, $endPos - $startPos + strlen($end));
	
	$returnText = substr ($original, 0, $startPos);
	if ($include_more_tag && !$include_after)
		$returnText.= $moreTag;
	$returnText.= '<p>'.$replace.'</p>';
	if ($include_more_tag && $include_after)
		$returnText.= $moreTag;
	$returnText.= substr ($original, $startPos + strlen($moreTag));
	
	return $returnText;
}

function	
get_post_middle_ads () {
return <<<EOT
// Your advertisment script here
EOT;
}

And this is the way it works in the site.

More from my site

  • Google Adsense Search Result in WorpressGoogle Adsense Search Result in Worpress
  • How to Add Advertisement Code to WordPress Theme’s SidebarHow to Add Advertisement Code to WordPress Theme’s Sidebar
  • Wordpress: How To Change Admin UsernameWordPress: How To Change Admin Username
  • Wordpress: Transfer Your Blogs To Another Hosting With Minimum DowntimeWordPress: Transfer Your Blogs To Another Hosting With Minimum Downtime
  • Wordpress: Version 2.9WordPress: Version 2.9
  • Wordpress: Catch A Spam Comment With StatcounterWordPress: Catch A Spam Comment With Statcounter

Filed Under: Programming and Scripting Tagged With: Adsense, advertisement, PHP, theme, Wordpress

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Leave a Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Software and Hardware

MD5 and SHA1 Checksum Using Windows

July 5, 2017 By Sze Hau Leave a Comment

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in