Support Log in

46. Custom Field Types

Developer Guide
Prefer video? Watch the Field Forge tutorials 8 short guides covering every feature Watch

Field Forge has no API for adding a field type of your own. The list of types is fixed in the plugin and nothing filters it, so a class that registers a type through fieldforge/register_field_types, fieldforge/field_types, fieldforge/render_field/type=... or fieldforge/format_value/type=... does nothing: Field Forge never fires those hooks.

What to use instead

Pick the built-in type that stores what you need and shape the output in the template. A star rating, for example, is a Number field named rating with Min 1 and Max 5 (Field Forge > Field Groups, the Number type has Min, Max and Step settings). The stars are plain template code:

php
// In a template
$rating = max( 0, min( 5, (int) get_field( 'rating' ) ) ); // 0 when the field is empty
echo str_repeat( '★', $rating ) . str_repeat( '☆', 5 - $rating );

Changing a value as it is loaded

The fieldforge/load_value filter runs on every stored value that Field Forge reads. It receives the value, the field name, the post ID and the stored row, and returns the value that get_field() gives back:

php
add_filter( 'fieldforge/load_value', function ( $value, $field_name, $post_id, $row ) {
    if ( 'rating' === $field_name ) {
        return max( 0, min( 5, (int) $value ) );
    }
    return $value;
} , 10, 4 );

The filter changes what is read, not how the field is edited: the editing control in the admin is the one of the built-in type.

—

Forge AI Assistant Online

Hi! I'm the Field Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs