Adding the parameters to our plugin
Our plugin works well, but what if the phone number format in your country is not 1234-5678? For example, Germany uses the format 123-45678. Wouldn't it be better if we had a parameter where we could set the phone number format? As this is a simple plugin, we are not going to worry about area code prefix or numbers in more complex formats such as 123-456-7890; we are concentrating on numbers that consist of two groups of digits.
We are going to add two parameters to our plugin (also known as options), one that sets the number of digits in the first group, and one that sets the number of digits in the second group.
So open up your clicktocall.xml
file, and add in the highlighted code shown as follows (everything between the config tags):
<?xml version="1.0" encoding="UTF-8"?> <extension version="3.0" type="plugin" group="content" method="upgrade"> <name>Content - Click To Call</name> <author>Tim Plummer</author> <creationDate>April 2013</creationDate> <copyright>Copyright (C) 2013 Packt Publishing. All rights reserved.</copyright> <license>http://www.gnu.org/licenses/gpl-3.0.html</license> <authorEmail>example@packtpub.com</authorEmail> <authorUrl>http://packtpub.com</authorUrl> <version>1.1.0 </version> <description>This plugin will replace phone numbers with Click to Call links. Requires Joomla! 3.0 or greater. Don't forget to publish this plugin! </description> <files> <filename plugin="clicktocall">clicktocall.php</filename> <filename>index.html</filename> </files> <config> <fields name="params"> <fieldset name="basic"> <field name="phoneDigits1" type="text" default="4" label="Digits 1" description="How many digits in the first part of the phone number?" /> <field name="phoneDigits2" type="text" default="4" label="Digits 2" description="How many digits in the second part of the phone number?" /> </fieldset> </fields> </config> </extension>
Besides incrementing the version number, we have added two text fields that default to 4
which was our original number of digits.
<field name="phoneDigits1" type="text" default="4" label="Digits 1" description="How many digits in the first part of the phone number?" />
If you take a look in your plugin through the plugin manager, you will see these new parameters added on a Basic Options tab. But we still need to edit our PHP file to actually use these parameters.
Open up your clicktocall.php
file, and make the following changes to your clickToCall
function.
protected function clickToCall(&$text, &$params) { $phoneDigits1 = $this->params->get('phoneDigits1', 4); $phoneDigits2 = $this->params->get('phoneDigits2', 4); // matches 4 numbers followed by an optional hyphen or space, then followed by 4 numbers. // phone number is in the form XXXX-XXXX or XXXX XXXX $pattern = '/(\W[0-9]{'.$phoneDigits1.'})-? ?(\W[0-9]{'.$phoneDigits2.'})/'; $replacement = '<a href="tel:$1$2">$1$2</a>'; $text = preg_replace($pattern, $replacement, $text); return true; }
We are loading up the phoneDigits1
parameter and putting it into a $phoneDigits1
variable which we use in the pattern line. Notice that we are loading a default value of 4
just in case our parameters do not load correctly or are not set.
$phoneDigits1 = $this->params->get('phoneDigits1', 4);
Now, assuming you set the digit parameters to 3
and 5
as per the previous screenshot, you can add a phone number in the format 123-45678 into one of your articles and you should see that they are now getting changed to a Click to Call link. , as in the following screenshot:
So now your plugin is much more flexible as the number of digits is not hardcoded, and anyone using your plugin can make this minor change without having to touch any code.
If you wanted to make this plugin even better, you could make it recognize multiple phone number formats, because as you can see, if you set it to recognize the phone number format 123-45678, then it no longer changes the 1234-5678 numbers to Click to Call.