Here you can find some code snippets to use with Greyd Forms Filters.
Modifying form validation
PHP
add_filter( 'greyd_forms_required_fields', function( $required_fields, $post_id, $data ) {
// Add custom required field logic
if ( $post_id === 123 ) {
$required_fields[] = 'my-custom-meta-field';
}
return $required_fields;
}, 10, 3 );Render a custom field at the end of a form
PHP
add_action( 'greyd_forms_render_form_end', function( $post_id ) {
if ( $post_id === 123 ) {
echo "<input type='hidden' name='my-custom-meta-field' value='" . get_post_meta( $post_id, 'my-custom-field', true ) ."'>";
}
} );Customizing email content
PHP
add_filter( 'greyd_forms_admin_mail_content', function( $content, $post_id, $data, $token ) {
// Add custom content to admin emails
$content .= '<p>Custom notification content</p>';
return $content;
}, 10, 4 );Modifying form classes
PHP
add_filter( 'greyd_forms_css_classes', function( $classes, $post_content, $post_id ) {
// Add custom CSS classes based on form content
if ( strpos( $post_content, 'business' ) !== false ) {
$classes['business_form'] = 'business-form';
}
return $classes;
}, 10, 3 );Customizing input field lengths
PHP
add_filter( 'greyd_forms_input_maxlength_text', function( $maxlength ) {
// Set custom maxlength for text fields
return 500;
}, 10, 1 );
add_filter( 'greyd_forms_input_maxlength_all', function( $maxlength ) {
// Set global maxlength for all fields
return 2000;
}, 10, 1 );Modifying hidden field values
PHP
add_filter( 'greyd_forms_hidden_field_value', function( $value, $name, $attrs ) {
// Customize hidden field values
if ( $name === 'user_id' ) {
$value = get_current_user_id();
}
return $value;
}, 10, 3 );Add required fields to a form
PHP
/**
* Filter the required fields for form submissions.
* Eg. Add the custom field 'my_custom_field' to the required fields for a specific form.
*
* @param array $required_fields Array of required field names.
* @param int $form_id The WP_Post ID of the form being submitted.
* @param array $form_data Validated form data.
*/
function greyd_forms_required_fields_example( $required_fields, $form_id, $form_data ) {
// Get the form post object
$form = get_post( $form_id );
// Check if the form is the one we want to allow the custom field for
if ( $form && $form->post_name === 'my-contact-form' ) {
// Add the custom field 'my_custom_field' to the required fields
$required_fields[] = 'my_custom_field';
}
return $required_fields;
}
add_filter( 'greyd_forms_required_fields', 'greyd_forms_required_fields_example', 10, 3 );Add an error message based on field values.
PHP
/**
* Add custom error messages for form submissions, after validation.
* Eg. Add custom error messages for the custom field 'my_custom_field' for a specific form.
*
* @param array $errors Current form errors.
* @param array $form_data Validated form data.
* @param int $form_id WP_Post ID of the form.
*
* @return array $errors
*/
function greyd_forms_handler_errors_example( $errors, $form_data, $form_id ) {
// Get the form post object
$form = get_post( $form_id );
// Check if the form is the one we want to add custom error messages for
if ( $form && $form->post_name === 'my-contact-form' ) {
// Check the custom field 'my_custom_field' and perform custom validation
$my_custom_field_value = isset( $form_data['my_custom_field'] ) ? $form_data['my_custom_field'] : null;
if ( ! $my_custom_field_value ) {
$errors['my_custom_field'] = 'Please enter a value for the custom field.';
}
else if ( strlen( $my_custom_field_value ) < 5 ) {
$errors['my_custom_field'] = 'The custom field must be at least 5 characters long.';
}
}
return $errors;
}
add_filter( 'greyd_forms_handler_errors', 'greyd_forms_handler_errors_example', 10, 3 );Adjust the validated data of a form
PHP
/**
* Filter the validated form data before submission.
* Eg. modify the validated form data before it is saved to the entry.
*
* @param array $form_data The validated form data.
* @param int $form_id The WP_Post ID of the form being submitted.
*
* @return array The validated form data.
*/
function greyd_forms_filter_validated_data_example( $form_data, $form_id ) {
// Get the form post object
$form = get_post( $form_id );
// Check if the form is the one we want to add custom data for
if ( $form && $form->post_name === 'my-contact-form' ) {
// Check the custom field 'my_custom_field' and perform custom validation
$my_custom_field_value = isset( $form_data['my_custom_field'] ) ? $form_data['my_custom_field'] : null;
if ( $my_custom_field_value ) {
$form_data['my_custom_field'] = sanitize_text_field( $my_custom_field_value ) . ' (custom data added)';
}
}
return $form_data;
}
add_filter( 'greyd_forms_validated_data', 'greyd_forms_filter_validated_data_example', 10, 3 );