The Themosis framework is bundled with an API to build WordPress custom fields. The framework gives you a list of predefined fields to use that leverage WordPress core API in order to customize your content.
The framework handles the display of your custom fields in the WordPress administration.
The Field class is an helper class in order to build custom fields in WordPress. It's a generic class that you should use to build any type of custom fields.
This class is used for:
The Field class implements the Form class to output its inputs but also provides a UI regarding the context: metabox, settings, user, ...
Here is the list of custom fields available:
Build a simple text field: <input type="text"/>
Field::text($name, $features = [], $attributes = []);
Possible values for the $features parameter:
$features = [
'title' => 'The field display title. By default it uses the $name.',
'info' => 'Add a helper text/description to the field.',
'default' => 'You can define a default value for the field.'
];
Some fields implement a parameter called $attributes. This parameter allows you to specify custom attributes to the field HTML tag.
For example, let's add a custom class to a text field:
Field::text('author', ['title' => 'Book author'], ['class' => 'custom-text-class']);
You can add any attributes you want each time you see a field implementing the $attributes parameter.
Build a textarea field: <textarea></textarea>
Field::textarea($name, $features = [], $attributes = []);
Build a simple number field: <input type="number"/>
Field::number($name, $features = [], $attributes = []);
Build a simple date field: <input type="date"/>
Field::date($name, $features = [], $attributes = []);
Build a simple password field: <input type="password"/>
Field::password($name, $features = [], $attributes = []);
Build a color field and display a color picker widget so users can pick a color easily.
Field::color($name, $features = [], $attributes = []);
Build a single or multiple checkbox field: <input type="checkbox"/>
Field::checkbox($name, $options, $features = [], $attributes = []);
In the example below, let's define a checkbox with one option. This might be a toggle option:
Field::checkbox('toggle', 'activate');
The above code will output a checkbox field with displayed label of Activate and a value of activate.
If you want to customize the text displayed beside the checkbox, set a key/value pair for your checkbox options like so:
Field::checkbox('toggle', ['activate' => 'Activate this option']);
The code will output the checkbox like the previous example except that its text beside the checkbox is now customized.
The checkbox field renders a label tag beside each of its inputs. You can now pass custom attributes to those label tags by passing a label attribute to the field attributes property:
Field::checkbox('toggle', 'activate', [], ['label' => ['class' => 'label-class']]);
Pass an array of attributes to the label special property of the checkbox field.
Simply pass an array of choices/options to your checkbox field like so:
Field::checkbox('colors', ['red', 'green', 'blue']);
Build radio field: <input type="radio" />
The radio field also provides multiple radio inputs with a label beside each input. The label attribute is also available to the radio field and you can also customize the displayed text for each option/choice of your field.
Field::radio($name, $options, $features = [], $attributes = []);
label attribute is also available. Read the checkbox field documentation above on how to use it.Build a select field: <select></select>
The select field is highly customizable. You can build simple select tag or with subgroups or/and define if you need multiple selection.
You can also define the value of your option tag to be numeric or custom: <option value="$key">$value</option>
Field::select($name, $options, $features = [], $attributes = []);
Note: If you want to make multi selection field, simply add the
multipleattribute. Previous release proposed a$multipleargument which is deprecated in favor of the$attributes['multiple']parameter.
Build a simple select field with numeric values:
Field::select('my-field', [
[
'None',
'Belgium',
'France',
'United States'
]
], ['title' => 'Choose a country:']);
Build a simple select field with custom values:
Field::select('my-field', [
[
'none' => 'None',
'bel' => 'Belgium',
'fra' => 'France',
'usa' => 'United States'
]
], ['title' => 'Choose a country:']);
Build a select field with subgroup of options using numeric values:
Field::select('my-field', [
'europe' => [
'Belgium',
'France'
],
'america' => [
'United States'
]
]);
Build a select field with subgroup of options using custom values:
Field::select('my-field', [
'europe' => [
'bel' => 'Belgium',
'fra' => 'France'
],
'america' => [
'usa' => 'United States'
]
]);
Simply set the multiple attribute of the select field like so:
Field::select('my-field', [
[
'None',
'Belgium',
'France',
'United States'
]
], ['title' => 'Choose a country:'], ['multiple']);
Build a WordPress Editor TinyMCE field.
Note: This field isn't actually working with the Infinite custom field.
Field::editor($name, $features, $settings);
Build a media field. This field allows you to upload or attach any file by using the WordPress Media API.
Field::media($name, $features = []);
By default, the media field allows you to attach images only.
Field::media('profile');
In order to allow the media field to attach other files except images, you need to specify the type feature like so:
Field::media('report', [
'title' => 'Attach report',
'type' => 'application'
])
The
typefeature only accepts 4 values:image,application,videoandaudio.
Allows a user to add/edit/delete a collection of media files. The collection field UI allows an end-user to add, order, delete or bulk delete files. Useful for galleries or specifying a list of files to download, ...
Field::collection($name, $features = []);
limit property which allows you to limit the number of media a user can add to the collection.// The default collection field allows you to manage images.
Field::collection('gallery');
// Same but only for files.
Field::collection('papers', ['type' => 'application']);
Using the limit feature property, you can limit the number of media files a user can add to the collection field:
// Limit the number of media to add to a collection.
Field::collection('pics', ['limit' => 5]);
Allowed
typevalues:image,application,video,audio.
Build a repeatable field. This allows you to define a single field or a group of fields to be repeated.
Field::infinite($name, $fields, $features);
Field class excepts the editor field.Please note that Infinite fields can't be nested currently.
Example of an infinite field:
Field::infinite('books', [
Field::text('title'),
Field::textarea('excerpt'),
Field::media('cover-image')
]);
You can limit the number of repeatable rows by setting the limit property like so:
Field::infinite('books', [
Field::text('title'),
Field::media('image')
], [
'title' => 'List of books',
'limit' => 10
])
The above code has a limit set to 10 rows of books.
Check those guide to implement your custom fields:
Made in Belgium