Fields
In HivePress, field types are implemented as PHP classes with properties and methods that determine the validation and rendering of the field HTML. For example, the Email
field type renders a text input that accepts email addresses only. You can use any of the existing field types in forms or even create a new one if required.
Creating field types
If you are developing a custom HivePress extension, you may need to create a new field type. To do this, create a new class-{field-type}.php
file (use lowercase letters, numbers, and hyphens only) in the includes/fields
extension subdirectory and HivePress will load it automatically.
It’s good practice to re-use the existing HivePress field types and avoid creating new ones if possible. For example, a new type may be required if you need a completely new UI element, or if it requires some custom logic.
Then, define the field type PHP class. The class name should be based on the file name but with underscores instead of hyphens and no lowercase restriction (e.g. Foo_Bar
class for the class-foo-bar.php
file). Pick a name that is unique enough to avoid conflicts with other HivePress field types.
The code example above implements a field type that normalizes values by trimming whitespace and removing slashes, then sanitizes them by removing HTML and special characters, and finally validates values based on its max_length
property (set to 123
by default). The field itself is rendered as an HTML input of text
type.
Also, a custom field type can be based on another type's PHP class, allowing you to re-use its properties and methods without repeating the code:
In the same way, you can create custom field types for your HivePress extension.
Customizing field types
You can customize any of the existing field types using hooks. For example, the code below changes the default date format for the Date
field type.
Similarly, you can customize any field type in HivePress or its extensions.
Last updated