Adding a field type
A complete example that registers a field type of your own, with its own check, in both checkouts.
Fieldline keeps a list of field types, and other plugins can add to it with the fieldline_field_types filter. A new type shows up in the Type dropdown, works in both checkouts, and its answers show on orders like any other.
What a type is
Each type says how it looks in each checkout:
| Key | Meaning | Default |
|---|---|---|
label |
The name in the Type dropdown (required) | |
block |
The Checkout block's field type: text, select or checkbox, the only three it has |
text |
classic |
The classic checkout's woocommerce_form_field() type, such as text, textarea, select, radio, tel, url or date |
text |
options |
Whether the type has options (value => label), entered on the field's screen | false |
placeholder |
Whether the field's screen offers a placeholder | true |
Fieldline's own types (text, textarea, select, checkbox) can't be replaced.
Example: a website address
/**
* Plugin Name: Website field for Fieldline
*/
add_action( 'fieldline_loaded', function () {
// The type.
add_filter( 'fieldline_field_types', function ( $types ) {
$types['website'] = array(
'label' => __( 'Website address', 'my-plugin' ),
'block' => 'text',
'classic' => 'url',
);
return $types;
} );
// Let browsers fill it in, in the Checkout block.
add_filter( 'fieldline_block_field', function ( $args, $field ) {
if ( 'website' === $field['type'] ) {
$args['attributes'] = array( 'autocomplete' => 'url' );
}
return $args;
}, 10, 2 );
// Check it, in both checkouts.
add_filter( 'fieldline_value_problem', function ( $problem, $field, $value ) {
if ( '' === $problem && 'website' === $field['type'] && ! wp_http_validate_url( $value ) ) {
/* translators: %s: field label */
return sprintf( __( '%s must be a web address, starting with https://.', 'my-plugin' ), $field['label'] );
}
return $problem;
}, 10, 3 );
} );
Activate it, and Website address is in the Type dropdown on WooCommerce → Checkout Fields.
If your plugin is deactivated
Fields of your type are kept, with all their settings and answers, but Fieldline leaves them out of checkout until a plugin registers the type again. The list of fields says so under the field's name. That way a field is never shown in a shape it wasn't made for.
Settings of your own
To store a setting on a field (say, a list of allowed domains for the website field), add a row to the field's screen with fieldline_field_rows, read it in fieldline_posted_field, and clean it in fieldline_clean_field. See Filters and hooks.