Exclusive WordPress
Just another blog from a WordPressholic.

Simple Plugin to Inject Ad Code within Blog Post

July 7th 2009 in Tips and Tricks, WordPress Plugins by Poer

There are a lot of plugin to inject ad code within blog post out there, but most of them is packed with many features that I don’t really need.

If you like me, just need a simple code to inject the ad code inside the blog post in random position, you can use the following code to achieve that.

You can add the code within your template function.php or download the plugin version here.

<?php
function pk_adsense_injection($content){
if (is_single()){
$adsense_code = '<div class="adcontainer">PUT YOUR AD CODE HERE</div>';
$paragraphs = explode('</p>',$content);
$i = mt_rand(1, count($paragraphs)-1);
$ix = 1;
foreach($paragraphs as $p)  {
$temp .= $p;
if ($ix == $i)
$temp .= $adsense_code;
$ix += 1;
}
$content = $temp;
}
return $content;
}
add_filter('the_content','pk_adsense_injection');
?>

Please do not forget to replace ‘PUT YOUR AD CODE HERE’ with your ad code (adsense, adbrite, clickbank etc).


One comment to...
“Simple Plugin to Inject Ad Code within Blog Post”
Avatar
david

thanks for the help
it really works without plugin needs which takes time too much time to load
thanks

david´s last blog… Easy Ways To Make Money Online


Leave a Reply



required



required - won't be displayed


Your Comment:

An update mainly to fix several bugs found, add more options for the display format, and record search engine visitor coming to blog page too.

Bugs fixed in this version are:

Make sure the plugin is Case Insensitive, and make sure only one search term counter updated when search engine visitor using search term that already saved [...]

Previous Entry

This one just for my note, a simple linux command line to search and replace text and code within files recursively. Simple, just oneliner, but this oneliner really help me much.

Using combination of Grep and Sed command, the following linux command line will search for ‘http://abc.wordpress.com’ code and replace it with ‘http://abcd.wordpress.com’ (without single quote).

grep [...]

Next Entry