InMotion Hosting Support Center

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
n/a Points
2015-03-02 4:30 pm

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??

Staff
29,167 Points
2015-03-02 5:06 pm
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

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.

2 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!