Filter
uncanny-automator-pro
automator_pro_formatter_locale
Filters the site locale used by the formatter integration before formatting data.
add_filter( 'automator_pro_formatter_locale', $callback, 10, 3 );
Description
Allows developers to filter the locale used by the Date Formatter in Uncanny Automator Pro. This hook enables overriding the default site locale with a custom one, providing flexibility for date formatting across different languages and regions within your automations. The `$site_locale`, `$from_format`, and `$to_format` parameters are passed for context.
Usage
add_filter( 'automator_pro_formatter_locale', 'your_function_name', 10, 3 );
Parameters
-
$site_locale(mixed) - This parameter contains the current WordPress site locale.
-
$from_format(mixed) - This parameter contains the current WordPress site locale, which influences date and time formatting.
-
$to_format(mixed) - This parameter holds the original format of the date input, which is crucial for correctly parsing the input date.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the 'automator_pro_formatter_locale' filter hook.
* This example forces the locale to be 'en_US' for date formatting,
* regardless of the site or user's current locale.
*/
add_filter( 'automator_pro_formatter_locale', 'my_automator_force_en_us_locale', 10, 3 );
/**
* Forces the locale to 'en_US' for Uncanny Automator Pro's date formatting.
*
* @param string $site_locale The default site locale.
* @param mixed $from_format The original date format.
* @param mixed $to_format The desired date format.
*
* @return string The locale to use for formatting.
*/
function my_automator_force_en_us_locale( $site_locale, $from_format, $to_format ) {
// We want to always use 'en_US' for date formatting in this specific scenario.
// This might be useful for consistency in logs or external integrations
// where a predictable date format is crucial.
return 'en_US';
}
// Example of how the filter might be used internally by Uncanny Automator Pro:
// (This part is illustrative and not part of the filter definition itself)
/*
function example_internal_usage( $timestamp, $from_format, $to_format ) {
$site_locale = get_locale();
$current_locale = determine_locale();
// The filter 'automator_pro_formatter_locale' is applied here.
// If 'my_automator_force_en_us_locale' is active, it will return 'en_US'.
$final_locale = apply_filters( 'automator_pro_formatter_locale', $site_locale, $from_format, $to_format );
switch_to_locale( $final_locale );
$output = wp_date( $to_format, $timestamp );
switch_to_locale( $current_locale ); // Switch back to the original locale
return $output;
}
*/
?>
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
uncanny-automator-pro/src/integrations/formatter/actions/date-formatter.php:126
public function format( $input, $from_format, $to_format ) {
$date_object = $this->date_object( $input, $from_format );
$timestamp = $date_object->getTimestamp();
$site_locale = get_locale();
// The current locale is set to user's locale by default and used in the wp_date function. We want to force site locale instead for consistency.
$current_locale = determine_locale();
// Force site locale but allow users to override this.
switch_to_locale( apply_filters( 'automator_pro_formatter_locale', $site_locale, $from_format, $to_format ) );
$output = wp_date( $to_format, $timestamp );
//Switch back to the initial locale.
switch_to_locale( $current_locale );
return apply_filters( 'automator_pro_formatter_format_date', $output, $input, $from_format, $to_format, $date_object );
}