Greyd Forms Filter Snippets

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 );

Frontend Post Forms: Adjust Permissions

PHP
/**
* Filter configuration errors and permission to create posts.
* Can be used for additional verification of the user.
* Any return value other than 'false' will deny the creation of the post.
*
* @filter greyd_forms_posts_interface_permission
*
* @param bool|string  $error     false if no config errors occured or error message.
* @param int    $entry_id        WP_Post ID of the entry.
* @param array  $formdata        Validated form data.
* @param array  $postmeta        The form post_meta for this specific interface.
*/
function posts_interface_additional_permission_check( $error, $entry_id, $formdata, $postmeta ) {

  // get ID of submitted form
  $form_id = get_post_meta( $entry_id, 'tp_form_id', true );
  if ( $form_id != 1234 ) {
    // only filter submissions of form '1234'
    return $error;
  }

  // check if user is admin
  if ( !current_user_can( 'administrator' ) ) {
    $error = __( "User is not admin", 'greyd_forms' );
    $error = __( "Unable to create post", 'greyd_forms' ) . ' (' . $error . ')';
  }

  return $error;
}
add_filter( 'greyd_forms_posts_interface_permission', 'posts_interface_additional_permission_check', 10, 4 );

Frontend Post Forms: Additional processing of form fields

PHP
/**
* Filter the converted form data used to create the Post array.
*
* @filter greyd_forms_posts_interface_data
*
* @param array  $interface_data  Converted form data.
* @param int    $entry_id        WP_Post ID of the entry.
* @param array  $formdata        Validated form data.
* @param array  $postmeta        The form post_meta for this specific interface.
*/
function posts_interface_filter_interface_data( $interface_data, $entry_id, $formdata, $postmeta ) {

  // get ID of submitted form
  $form_id = get_post_meta( $entry_id, 'tp_form_id', true );
  if ( $form_id != 1234 ) {
    // only filter submissions of form '1234'
    return $interface_data;
  }

  // make new posts a child of post '321'
  $interface_data['post_parent'] = 321;

  return $interface_data;
}
add_filter( 'greyd_forms_posts_interface_data', 'posts_interface_filter_interface_data', 10, 4 );

Frontend Post Forms: Additional processing of post data

PHP
/**
* Filter the Post array used to make a post.
*
* @filter greyd_forms_posts_interface_postarray
*
* @param array  $postarr         Post array to make a post from.
* @param array  $interface_data  Converted form data.
* @param int    $entry_id        WP_Post ID of the entry.
* @param array  $formdata        Validated form data.
* @param array  $postmeta        The form post_meta for this specific interface.
*/
function posts_interface_filter_postarray( $postarr, $interface_data, $entry_id, $formdata, $postmeta ) {

  // get ID of submitted form
  $form_id = get_post_meta( $entry_id, 'tp_form_id', true );
  if ( $form_id != 1234 ) {
    // only filter submissions of form '1234'
    return $postarr;
  }

  // add custom metadata to new post
  if ( empty($postarr['meta_input'] ) ) $postarr['meta_input'] = array();
  $postarr['meta_input']['my_custom_meta'] = sprintf( __( "Created with %s", 'greyd_forms' ), $form_id );

  return $postarr;
}
add_filter( 'greyd_forms_posts_interface_postarray', 'posts_interface_filter_postarray', 10, 5 );