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

Continued Education in Course 215: Joomla 2.5 Content Plugin Development
You are viewing Section 13: Use onContentPrepare to change Article text in a Joomla 2.5 plugin
Section 12: Joomla 2.5 content plugin variables
Section 14: How to add a css StyleSheet to a Joomla 2.5 content plugin
Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

0 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!