Skip to content

Instantly share code, notes, and snippets.

@josueochoa
Created September 13, 2013 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josueochoa/6553853 to your computer and use it in GitHub Desktop.
Save josueochoa/6553853 to your computer and use it in GitHub Desktop.
<?php
function the_content_ad($content){
if(is_single()){
$switch = 1;$cont_ad = 0;$parrafo = 0;
for($i=0;$i<strlen($content);$i++)
{
if(substr($content,$i,4)=="</p>"){
$parrafo++;
}
if(substr($content,$i,6)=="</pre>"){
$parrafo++;
}
if(substr($content,$i,13)=="</blockquote>"){
$parrafo++;
}
if(substr($content,$i,8)=="</table>"){
$parrafo++;
}
if(substr($content,$i,5)=="</ul>"){
$parrafo++;
}
}
if($parrafo <= 3){
$put = (int)($parrafo/2);$ad=0;
}else if($parrafo > 3 and $parrafo <= 8){
$put = (int)($parrafo/2);$ad=1;
}else if($parrafo > 8){
$put = (int)($parrafo/3);$ad=2;
}
for($i=0;$i<strlen($content);$i++)
{
if(substr($content,$i,4)=="</p>" or substr($content,$i,6)=="</pre>" or substr($content,$i,13)=="</blockquote>" or substr($content,$i,8)=="</table>" or substr($content,$i,5)=="</ul>"){
if($switch == $put and $cont_ad != $ad){
if(substr($content,$i,4)=="</p>"){
$tag = "</p>";
$i +=3;
}
if(substr($content,$i,6)=="</pre>"){
$i +=5;
$tag = "</pre>";
}
if(substr($content,$i,13)=="</blockquote>"){
$i +=12;
$tag = "</blockquote>";
}
if(substr($content,$i,8)=="</table>"){
$i +=7;
$tag = "</table>";
}
if(substr($content,$i,5)=="</ul>"){
$i +=4;
$tag = "</ul>";
}
$text = $text.$tag ."<div id='ad_".($cont_ad+1)."' class='ad_box'></div>";
$cont_ad += 1;
$switch = 1;
}else{
$text = $text."".substr($content,$i,1);
$switch += 1;
}
}else{
$text = $text."".substr($content,$i,1);
}
}
return $text;
}else{
return $content;
}
}
add_filter('the_content','the_content_ad');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment