Simple Plugin to Inject Ad Code within Blog Post

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).

Incoming search engine terms:

make simple plugin wordpress

2 comments

  1. 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

  2. make adsense plugin aja mas aku,,,,,whydoworkadsense

Leave a Reply