Cybo Advanced Field EngineCyboweb Technologies · Documentation · v1.0.2

Form hooks

Every form hook, in the order it runs, with an example. The full signatures are in Hooks & Filters → Dynamic forms. $form is an Cybo\AdvancedFieldEngine\Forms\FormDefinition (read-only: id, object_type, object_id, mode, post_type, field_groups …).

While showing the form

Hook Type Use it to
cafe_dynamic_form_args filter add defaults or translate texts of forms
cafe_dynamic_form_can_submit filter allow or refuse people (e.g. "submissions closed")
cafe_dynamic_form_fields filter remove (or add) fields: the server accepts the same list
cafe_dynamic_form_before_fields filter HTML above the fields
cafe_dynamic_form_after_fields filter HTML below the fields (consent text, CAPTCHA)
cafe_dynamic_form_submit_button filter your own button
cafe_dynamic_form_html filter wrap or change the whole form
cafe_dynamic_form_field_description filter what REST clients get per field

While sending

Hook Type Use it to
cafe_dynamic_form_before_validate action log or rate-limit attempts
cafe_dynamic_form_validate filter checks across fields
cafe_dynamic_form_failed action log refused submissions
cafe_dynamic_form_before_save action last look before saving
cafe_dynamic_form_post_data filter change the WordPress post (title from fields, parent, template)
cafe_dynamic_form_saved action e-mails, webhooks, cache
cafe_dynamic_form_redirect filter where to go after saving

Field-level hooks of CAFE run too: cafe_validate_field, cafe_pre_save_value, cafe_before_save_field, cafe_after_save_field.

Examples

Title from two fields

For forms without a title input: build it from the (already valid) values.

add_filter( 'cafe_dynamic_form_post_data', function ( array $data, $form, array $values ) {
	if ( 'new-event' === $form->id ) {
		$data['post_title'] = sprintf( '%s, %s', $values['venue'] ?? '', $values['event_date'] ?? '' );
	}
	return $data;
}, 10, 3 );

A CAPTCHA

add_filter( 'cafe_dynamic_form_after_fields', function ( string $html, $form ) {
	return 'new-event' === $form->id ? $html . my_captcha_html() : $html;
}, 10, 2 );

add_filter( 'cafe_dynamic_form_validate', function ( array $errors, array $values, $form ) {
	if ( 'new-event' === $form->id && ! my_captcha_is_valid() ) {
		$errors[''] = 'Please confirm you are not a robot.';
	}
	return $errors;
}, 10, 3 );

Tell the editors

add_action( 'cafe_dynamic_form_saved', function ( int $object_id, $form ) {
	if ( 'new-event' === $form->id ) {
		wp_mail( get_option( 'admin_email' ), 'New event to review', get_edit_post_link( $object_id, 'raw' ) );
	}
}, 10, 2 );

Only on weekdays

add_filter( 'cafe_dynamic_form_can_submit', function ( $allowed, $form ) {
	if ( 'new-event' === $form->id && wp_date( 'N' ) >= 6 ) {
		return new WP_Error( 'weekend', 'Submissions open again on Monday.', [ 'status' => 403 ] );
	}
	return $allowed;
}, 10, 2 );