Filter uncanny-automator

automator_wsformlite_file_token

Filters the file token used for WS Form Lite submissions, allowing modification before the data is processed.

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

Description

Filters the file token value for WS Form Lite submissions. Developers can modify the generated file token string before it's used by Uncanny Automator, allowing for custom formatting or filtering of attached files. This hook fires after WS Form Lite files have been processed into a token.


Usage

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

Parameters

$value (mixed)
This parameter holds the processed file upload value, typically an empty string initially, which is then populated with the URLs of uploaded files.
$files (mixed)
This parameter contains the current value of the file upload token, which is typically an empty string before being populated with file URLs.

Return Value

The filtered value.


Examples

add_filter( 'automator_wsformlite_file_token', function( $value, $files ) {
	/**
	 * Example: Limit the number of file tokens to the first 3 files uploaded.
	 *
	 * This filter hook allows you to modify the output of the file token
	 * for WS Forms Lite. In this example, we're taking the comma-separated
	 * list of file names and truncating it if more than 3 files were uploaded.
	 *
	 * @param string $value  The current file token value (comma-separated file names).
	 * @param array  $files  The array of WS Forms Lite file upload data.
	 *
	 * @return string The filtered file token value.
	 */

	// If more than 3 files were uploaded, truncate the value.
	if ( count( $files ) > 3 ) {
		// Split the value into an array, take the first 3 elements, and join them back.
		$file_names = explode( ', ', $value );
		$value      = implode( ', ', array_slice( $file_names, 0, 3 ) );
		$value      .= ', ...'; // Indicate that there are more files.
	}

	return $value;
}, 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/integrations/ws-form-lite/tokens/ws-form-lite-tokens.php:212
uncanny-automator-pro/src/integrations/ws-form-lite/tokens/ws-form-lite-pro-tokens.php:147

public function get_file_upload_value( $files ) {
		$value = '';
		foreach ( $files as $file ) {
			if ( isset( $file['attachment_id'] ) && ! empty( $file['attachment_id'] ) ) {
				// Get the URL if public.
				$url = wp_get_attachment_url( $file['attachment_id'] );
				if ( empty( $url ) ) {
					// Give the admin URL if not public.
					$url .= admin_url( 'upload.php?item=' . $file['attachment_id'] );
				}
				$value .= $url . ', ';
			}
		}

		// Remove the trailing comma and space.
		$value = ! empty( $value ) ? rtrim( $value, ', ' ) : '';

		// Allow 3rd parties to filter the value.
		return apply_filters( 'automator_wsformlite_file_token', $value, $files );
	}

Scroll to Top