Add text to an article using template overrides
From Joomla! Documentation
| This recently added article requires a review |
This tip tells you how to change Joomla! to automatically add text to an article. Say, for example, that you want to have some standard text ("This text has been added automatically by Joomla!.") added to the end of each article that is submitted from the front end. Here is how you can do this.
- Create a template override for the file
<joomla_root>/components/com_content/views/article/tmpl/form.php- Create a folder called
<joomla_root>/templates/<your template>/html/com_content/article. For example, for the "beez" template this folder is called<joomla_root>/templates/beez/html/com_content/article. - Copy the standard
form.phpfile from the components folder above to the templates folder. Note: If you already have a form.php file in this folder, don't copy the file. Just edit it as indicated below.
- Create a folder called
- Edit the copy of the file you made in the override folder as follows. Find the line
This is line 49 in the 1.5.8 version of this file. Change this to the following:
var text = <?php echo $this->editor->getContent( 'text' ); ?>
var text = <?php $addToText = '<p>Joomla! automatically added this text.</p>'; if (strpos($this->article->text, $addToText) == 0) { $this->article->text .= $addToText; } echo $this->editor->getContent( 'text' ); ?>
This is checking that the text to be added is not already in the article. If it is not, then it is added to the article. Obviously, you would change the text in the variable $addToText to whatever you need.
That's it. Now, when you edit an article from the front end, this text will automatically be added. Note that this change does not affect editing from the back end. Also, since this is done as a template override, it only affects pages that use the overridden template.
For more information on template overrides, see the tip How to override the output from the Joomla! core.
