Filter Dynamic uncanny-automator

automator_sanitize_get_field_type_{dynamic}

> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Filters the dynamic field type for a given option code and its associated options before rendering.

add_filter( 'automator_sanitize_get_field_type_{dynamic}', $callback, 10, 3 );

Description

Sanitize and modify the field type for dynamic Automator fields. This filter allows developers to override or adjust the default field type, especially useful for custom field integrations. The `{dynamic}` placeholder will be replaced with the actual field type at runtime. Developers can leverage this hook to ensure correct type handling and add custom validation or transformations before a field type is finalized.


Usage

add_filter( 'automator_sanitize_get_field_type_{dynamic}', 'your_function_name', 10, 3 );

Parameters

$type (mixed)
This parameter represents the dynamically determined field type for a given option code and its associated options.
$option_code (mixed)
This parameter represents the current field type that is being considered or returned by the hook.
$options (mixed)
This parameter contains the specific code or key for the option being processed.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the automator_sanitize_get_field_type_{dynamic} filter.
 * This example demonstrates sanitizing a custom field type 'custom_textarea'
 * and ensuring it's a valid string.
 */
add_filter( 'automator_sanitize_get_field_type_custom_textarea', function( $type, $option_code, $options ) {
    // Ensure the type is a string, even if it was something else initially.
    $sanitized_type = sanitize_text_field( $type );

    // You could add additional checks here if needed. For example,
    // checking if $option_code or $options contain specific values
    // to apply different sanitization logic.

    // For this specific dynamic hook, we are expecting 'custom_textarea'
    // If for some reason it's not, fall back to a default or throw an error.
    if ( 'custom_textarea' !== $sanitized_type ) {
        // In a real-world scenario, you might want to log this unexpected value
        // or fall back to a more generic type like 'text'.
        error_log( "Unexpected field type found for option code {$option_code}. Expected 'custom_textarea', got '{$sanitized_type}'." );
        return 'text'; // Fallback to a safe default
    }

    // Return the sanitized type.
    return $sanitized_type;
}, 10, 3 ); // 10 is the priority, 3 is the number of arguments accepted by the callback.

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/core/lib/utilities/class-automator-utilities.php:726

public function maybe_get_field_type( $option_code, $options ) {
		// if nothing is set, return text
		if ( empty( $options ) || ! isset( $options['fields'] ) || ! isset( $options['fields'][ $option_code ] ) ) {
			return apply_filters( 'automator_sanitize_get_field_type_text', 'text', $option_code, $options );
		}

		// if tinymce is set to yes, return HTML
		if ( isset( $options['fields'][ $option_code ]['supports_tinymce'] ) && 'true' === (string) $options['fields'][ $option_code ]['supports_tinymce'] ) {
			return apply_filters( 'automator_sanitize_get_field_type_html', 'html', $option_code, $options );
		}

		// if markdown is set to yes, return HTML
		if ( isset( $options['fields'][ $option_code ]['supports_markdown'] ) && 'true' === (string) $options['fields'][ $option_code ]['supports_markdown'] ) {
			return apply_filters( 'automator_sanitize_get_field_type_markdown', 'markdown', $option_code, $options );
		}

		// No type found
		if ( ! isset( $options['fields'][ $option_code ]['type'] ) || empty( $options['fields'][ $option_code ]['type'] ) ) {
			return apply_filters( 'automator_sanitize_get_field_type_text', 'text', $option_code, $options );
		}

		// Return type
		$type = (string) $options['fields'][ $option_code ]['type'];

		return apply_filters( 'automator_sanitize_get_field_type_' . $type, $type, $option_code, $options );
	}


Scroll to Top