Snippet IT IT News, Programming, Internet and Blogging

5Apr/080

Replace Wordpress’ more tag with advertisement code

ADVERTISEMENTS

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.

Related Articles

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.