Use onContentPrepare to change Article text in a Joomla 2.5 plugin

Joomla 2.5 has reached its end of life as for 12/31/2014. Please be advised this may be a security risk to your website. You can view more information about the end of life here.

When writing a Joomla 2.5 content plugin, you can use the onContentPrepare event to change the text within an Article. While other events, such as onContentAfterTitle, are useful for adding text, onContentPrepare is best for modifying the text entirely.

In this plugin development tutorial, we’ll show you an example of how to change text using onContentPrepare.

Accessing Article Text using $row->text

Before you can manipulate any of the article’s text, you must first know where it is stored. In functions declared like the following:

public function onContentPrepare($context, &$row, &$params, $page = 0)

The $row object will hold the article’s text (and introtext). You can use the following to view the content:

$row->text

Modifying Article Content

To change article text, you can change the $row->text variable. Look at the following code:

public function onContentPrepare($context, &$row, &$params, $page = 0)
{
    $row->text = str_replace("a","A",$row->text);
}

In the above onContentPrepare function, we are replacing every lower-case a with a capital A. While this is not the best real world example, it does show you how to change the text of an article. As you can see in the screenshot below, all of the letter a’s have been changed to A.

onContentPrepare-to-change-text

0 thoughts on “Use onContentPrepare to change Article text in a Joomla 2.5 plugin

  1. Hello, your example is very helpfull!

    but what i have to do to change the a into A in entire page and not only in the article??

    1. Hello Spiros,

      An entire page is put together from different pieces. There is not a specific tool to adjust all the different pieces at once.

      Kindest Regards,
      Scott M

Was this article helpful? Join the conversation!

Server Madness Sale
Score Big with Savings up to 99% Off

X