A Moodle profile field that saves phone numbers you can actually use
A text profile field gets phone numbers in a dozen formats you cannot dial or message. Our free profilefield_phone saves them all in one international format.
Short answer: when a Moodle site collects phone numbers in a text profile field, it gets “0555 12 34 56”, “+213555123456”, “555-12-34-56” and “call me after 5”. None of that can go straight into an SMS tool, a WhatsApp list or a CRM. Our free plugin, profilefield_phone, accepts numbers the way people type them and saves one international format (E.164).
This is the fifth plugin in our series: one plugin for each Moodle plugin type, each one teaching DevLMS Studio, our AI plugin builder, how that type works.
What it does
- An admin adds a field of type Phone number and sets a default country code, for example 213 for Algeria.
- “0555 12 34 56”, “+213 555-12-34-56” and “00213 555 12 34 56” are all saved as
+213555123456. - Text that is not a phone number is refused, with an example of what to type.
- On the profile page the number is a tap-to-call link.
- Leave the default country code empty and only numbers with a + (or 00) are accepted.
- Italy, San Marino and the Vatican keep the leading 0 of landline numbers, as their numbering plans require.
It checks the shape and length of a number (8 to 15 digits, as E.164 allows), not whether that exact number exists. It supports Moodle 4.5 LTS to 5.3 and is GPL. The code is on GitHub.
How a profile field plugin works
From Moodle 5.3’s user profile code and its built-in text field:
- The plugin is a field type, not a field. Admins still create the fields, and every value is stored by core in one table, one text value per user per field.
field.class.phpholdsprofile_field_NAME. It adds the input, validates it, can change the value just before it is saved, and prints it on the profile. Validation should start from the parent’s, which handles “unique” fields.define.class.phpadds the type’s own settings to the admin form, stored in five spare columns (param1toparam5). Our default country code isparam1.- These classes are not autoloaded, so tests have to load them by path.
One thing our Behat test taught us: a custom field labelled “Mobile” collides with Moodle’s own “Mobile phone” field, and the test typed into the wrong one. That lesson is now a note in our knowledge base and in the guide.
What Studio learned
The profile field guide is now in DevLMS Studio’s builder, with the label trap included.
Need a profile field of your own, like a national ID with a checksum or a student number that must match your SIS? Describe it in DevLMS Studio and watch it built and tested in a real Moodle.
Need a Moodle plugin built or rescued?
Tell the scoping assistant what you need. You get a fixed price and a timeline in a few minutes, and an engineer reviews it within one business day.
Get a fixed quote