Saturday, 03 August 2013 22:43

how to replace a text with other texts or codes by joomla plugin

Written by 
Rate this item
(0 votes)

It's a very common requirment that you may want to
insert ad codes or some other texts in a joomla article
or elsewhere.I am going to show how it can be done so
that if you want some text like "{replace it}" will be
replaced by your code -

in your custom plugin use following event -

defined('_JEXEC') or die;
jimport( 'joomla.plugin.plugin' );
class plgContentHelloworld extends JPlugin
function onAfterRender() // for replacing any body tag
$app = JFactory::getApplication();
if( $app->isAdmin()) {
return false;

$type = JFactory::getDocument()->getType();
if( $type != 'html' ) {
return false;
$response = JResponse::getBody();
$replace = "{replace it}";
$response = str_replace($replace, '<div style="margin:2px;">scripts/codes
here</div>', $response);
return true;

{replace it} text will be replaced by the scripts/codes you
enter in the above str_replace method.

Hope that helped you very much,don't forget to share and like
this joomla post

Read 2903 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.