Filter uncanny-toolkit-pro

wpforms_field_data

Filters field data before it's processed, allowing modification of individual field information within a form.

add_filter( 'wpforms_field_data', $callback, 10, 2 );

Description

Fires when field data is being processed. Allows modification of individual field properties before they are used or displayed. Useful for dynamically altering field labels, default values, or other attributes based on form context or external data.


Usage

add_filter( 'wpforms_field_data', 'your_function_name', 10, 2 );

Parameters

$field (mixed)
This parameter contains data associated with a specific form field.
$form (mixed)
This parameter represents the individual field data being processed by the hook.

Return Value

The filtered value.


Examples

/**
 * Modify the 'Course ID' hidden field data.
 *
 * This example demonstrates how to conditionally alter the default value
 * of a hidden 'Course ID' field based on whether the user is logged in.
 *
 * @param array $field The field data array.
 * @param array $form  The form data array.
 * @return array The modified field data array.
 */
add_filter( 'wpforms_field_data', function( $field, $form ) {

	// Check if this is the specific hidden field we want to modify.
	// We're looking for a field with type 'hidden' and label 'Course ID'.
	// In a real-world scenario, you might use a more robust method to identify the field,
	// such as a specific custom class or an ID if you control the form creation.
	if ( isset( $field['type'] ) && $field['type'] === 'hidden' && isset( $field['label'] ) && $field['label'] === 'Course ID' ) {

		// Example: Conditionally change the default value based on user login status.
		if ( is_user_logged_in() ) {
			// If the user is logged in, set a specific course ID.
			// In a real plugin, this would likely be retrieved from user meta or an option.
			$field['default_value'] = 500; // Example logged-in course ID
		} else {
			// If the user is not logged in, perhaps set a default or placeholder ID.
			$field['default_value'] = 0; // Example guest course ID
		}

		// Example: Conditionally make the field required.
		// Let's say we want to ensure the Course ID is present if it's a logged-in user.
		if ( is_user_logged_in() ) {
			$field['required'] = 1;
		} else {
			$field['required'] = 0;
		}
	}

	// Always return the field array, whether modified or not.
	return $field;

}, 10, 2 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/wpf-lesson-topic-auto-complete.php:453
src/classes/wpf-lesson-topic-auto-complete.php:464
src/classes/wpf-lesson-topic-auto-complete.php:475
src/classes/wpf-lesson-topic-auto-complete.php:727
src/classes/wpf-lesson-topic-auto-complete.php:738
src/classes/wpf-lesson-topic-auto-complete.php:749

'id'            => 10001,
			'type'          => 'hidden',
			'label'         => 'Course ID',
			'label_disable' => 1,
			'default_value' => $course_id,
			'required'      => 0,
		);
		$field = apply_filters( 'wpforms_field_data', $field, $form );
		array_push( $form['fields'], $field );

		$field = array(
			'id'            => 10002,
			'type'          => 'hidden',
			'label'         => 'Lesson ID',
			'label_disable' => 1,

Scroll to Top