Rendering after 4 lines of content

Hello everyone,

I’m trying to figure out how to change the below code. The code is a Joomla plugin which inserts adverts in the main content body of the articles. Is there a way to change this so the plugin renders at the 4th line of content down in my article? Or is this not possible.

<?php
/**
* @version $Id: ads.php 1.0
* @copyright Helen Van-Hotson
* @license GNU/GPL,
* @author - http://www.catchthelight.co.uk/joomla-auto-ads

*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

$mainframe->registerEvent( 'onBeforeDisplayContent', 'plgContentads' );

function plgContentads( &$row, &$params, $page=0 )

{   
       
   
   // Hide ads in Modules - doesn't work with newsflash module
   if (@$row->content){
      return;
   }
   $view      = JRequest::getCmd('view');
   $menu =&JSite::getMenu();
   $item =&$menu->getActive();
      if ($view == 'article'){
   $plugin =& JPluginHelper::getPlugin('content', 'ads');
   $pluginParams    = new JParameter( $plugin->params );
   $excluded_Itemids = $pluginParams->get( 'excl_itemids',"" );
   if (trim($excluded_Itemids) != '') {
      $arr = preg_split("/\\s*,\\s*/", $excluded_Itemids);
      if (in_array($item->id, $arr)) {
         return ;
      }
   }
      $beforeOrAfter = $pluginParams->get('beforeOrAfter','before');
      $alignment = $pluginParams->get('alignment','right');
      $padding = $pluginParams->get('padding','4px');
      $adcode = $pluginParams->get('adcode'," ");
      $text = "<div style='float:".$alignment."; padding:".$padding." ; margin-top:50px;'>";
         $text .= "$adcode";
      }
      $text .= "</div>";
      
      if($beforeOrAfter == 'before'){
         $row->text = $text . $row->text;
      }else{
         $row->text .= $text;
      }

      return;
   
   }
   
   
      
      


?>

Could any PHP guru help me out? Thank you very much.

well nothing is ever simple it seems. Found a glitch whereby if there’s a image in my article, the new code doesn’t work but instead breaks the image link. :x Any advice would greatly appreciated.

Yep its possible. You will need to change


$beforeOrAfter = $pluginParams->get('beforeOrAfter','before');

to


$beforeOrAfter = $pluginParams->get('beforeOrAfter','in');

and alter the last few lines to:


if($beforeOrAfter == 'before'){
    $row->text = $text . $row->text;
}else
if($beforeOrAfter == 'after'){
    $row->text .= $text;
} else {
    $x = explode(".", $row->text);
    $x[3] = $x[3] . $text;
    $row->text = implode(". ", $x); 
} 

Tinker about with that but it should run ok

Tinkering always helps. Managed to get this working thanks a lot SpikeZ!

Changed bottom lines to:

if($beforeOrAfter == ‘before’){
$x = explode(“.”, $row->text);
$x[3] = $x[3] . $text;
$row->text = implode(“”, $x);
}

Plugin settings were then changed to “before” to pick up this snippet.

Thanks so much!

SpikeZ - Thank you so much for taking the time to help me out. Really appreciate it. I have added the changes in as per your instruction but the behavior of the render is still the same. Any ideas what could be causing this?

Hi SpikeZ,

It appears the scripting interupts the code.

For example:

Line 1 = text
Line 2 = text
Line 3 = text
Line 4 = text + image + advert script

Instead of rendering the plugin at the beginning of line 4, the plugin is created in the middle of the line. If that means in the middle of exisiting code for example “<img src=”" /> then that’s where it puts it… very strange.

Of course the output then reads half image and half plugin script.

The code above does it render on the number of paragraphs? Maybe a way around it?

No problem mate.
If the image is breaking then check in your output source where the browser is looking for it.
It might be that the paths are wrong to the image.