Filter uncanny-automator

automator_sanitize_get_field_type_markdown

Filters the markdown field type settings before they are saved, allowing modification of their values.

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

Description

Filters the field type when markdown is requested. This hook allows developers to conditionally alter the returned field type, for example, to convert markdown to HTML when tinymce support is enabled for the field. It fires after checking for tinymce support.


Usage

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

Parameters

$option_code (mixed)
This parameter represents the original, unsanitized value of the field type, which defaults to 'markdown' if not explicitly set.
$options (mixed)
This parameter holds the code of the option being processed to retrieve its field type.

Return Value

The filtered value.


Examples

/**
 * Sanitize and determine the field type, prioritizing HTML for Markdown support.
 *
 * This filter hook is used internally by Automator to determine the actual field
 * type to be rendered for a given option. If the field explicitly supports Markdown
 * and is configured as such, this function intercepts and returns 'html' instead,
 * allowing for HTML rendering of Markdown content.
 *
 * @param mixed $field_type The default field type, typically 'markdown' in this context.
 * @param mixed $option_code The unique code or identifier for the option field.
 * @param mixed $options     The array of all available option configurations.
 *
 * @return string The sanitized and determined field type, usually 'html' if Markdown is supported.
 */
add_filter( 'automator_sanitize_get_field_type_markdown', function( $field_type, $option_code, $options ) {

	// Check if the specific field configuration supports Markdown.
	if ( isset( $options['fields'][ $option_code ]['supports_markdown'] ) && 'true' === (string) $options['fields'][ $option_code ]['supports_markdown'] ) {
		// If Markdown is supported, we want to ensure it's rendered as HTML.
		// This allows for rich text formatting within the field.
		// We re-apply a filter specifically for HTML sanitization for consistency.
		return apply_filters( 'automator_sanitize_get_field_type_html', 'html', $option_code, $options );
	}

	// If the field doesn't explicitly support Markdown, return the original type.
	// This ensures that if the filter was applied for some other reason,
	// it doesn't incorrectly change the field type.
	return $field_type;

}, 10, 3 );

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:715

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