Do you need to add a new setting in your Joomla template? With a few lines of PHP and XML code you can add a text field to print custom text in your public site.
In this post, I'm going to share with you how to add a custom text field to a Joomla 3 template.
Before to start, be sure you're using a template that doesn't expect updates from the developer in future. If necessary, work the customizations in a duplicated one.
Step #1. Access Template Editor
- Go to Extensions
- Templates (on the left side)
- Your Template Details and Files
Step #2. Edit templateDetails.xml file
- Click templateDetails.xml
- Edit the code in the right side of the screen
To add a simple text field, use the code below inside the <fieldset> tag:
<field name="customfield" type="text" default="" label="Custom Field Label" description="Custom Field Alt Label" filter="string" />
- Set a unique name value. In my example I use "customfield”.
- Type a clear label and description.
- Default is optional. Set a default value when the field is saved empty in step 4.
- Filter is optional. In this case, it prevents to save html tags.
- Save changes when you're done.
Step #3. Edit index.php file
I want to display my custom field next to "© 2015 Joomla!!" text in the footer:
Let's find that place in the template to print the value from our new field.
- Click index.php
- Edit the code in the right side
Look for the right place in index.php file, and type some PHP code:
<?php echo $this->params->get('customfield'); ?>
Replace 'customfield' with the name of your custom field from step 2.
- Save and close.
Step #4. Test the new field
- Go to Extensions > Templates > Styles > Your template style
- Look for the new param and type some text
- Save when you're done
Step #5. End result
Go to your public site and see the new field in action: