Filter uncanny-automator

automator_ai_settings_meta

Filters settings meta for AI features, allowing modification before they are saved or displayed.

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

Description

Filters the meta variables used for AI settings templates. Developers can use this hook to modify or add custom meta data before AI settings templates are rendered. It's applied within the `get_template_file_content` method, affecting the `$vars` array.


Usage

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

Parameters

$vars (mixed)
This parameter contains an array of variables that will be passed to the template file for rendering AI settings.
$file (mixed)
This parameter contains an array of variables that will be passed to the template file for use in its content.

Return Value

The filtered value.


Examples

<?php
/**
 * Example usage of the 'automator_ai_settings_meta' filter hook.
 *
 * This filter allows you to modify the meta data that is passed to the AI settings template.
 * In this example, we'll add a custom meta key with a dynamic value.
 *
 * @param array  $vars The existing variables array passed to the template.
 * @param string $file The path to the template file being rendered.
 * @return array The modified variables array.
 */
add_filter( 'automator_ai_settings_meta', function( $vars, $file ) {

	// Check if we are in the context of a specific AI settings template if needed.
	// For this example, we'll apply it to all AI settings meta.

	// Add a custom meta field.
	// This could be a user ID, a timestamp, or any other relevant dynamic data.
	$vars['meta']['custom_ai_context'] = 'user_id_' . get_current_user_id() . '_' . time();

	// You could also conditionally modify existing meta data based on the $file.
	if ( strpos( $file, 'advanced-settings.php' ) !== false ) {
		if ( isset( $vars['meta']['api_key'] ) ) {
			// Example: Mask part of the API key for security in certain views.
			$vars['meta']['api_key'] = substr( $vars['meta']['api_key'], 0, 4 ) . '********' . substr( $vars['meta']['api_key'], -4 );
		}
	}

	// Always return the modified $vars array for filters.
	return $vars;

}, 10, 2 ); // Priority 10, accepts 2 arguments ($vars, $file)

?>

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/ai/adapters/integration/ai-settings.php:407

private function get_template_file_content( $file, $vars = array() ) {

		$vars['meta'] = apply_filters( 'automator_ai_settings_meta', $vars, $file );

		if ( ! file_exists( $file ) ) {
			throw new InvalidArgumentException( sprintf( 'Template file %s does not exist.', esc_html( $file ) ) );
		}

		ob_start();
		include $file;
		return ob_get_clean();
	}

Scroll to Top