Filter
uncanny-automator-pro
automator_pro_formatter_format_date
Filters the formatted date output for Automator Pro, allowing customization of date displays.
add_filter( 'automator_pro_formatter_format_date', $callback, 10, 5 );
Description
Fires after a date has been formatted by Uncanny Automator Pro's date formatter. Developers can filter the formatted date output, allowing for custom date formatting or manipulation before it's returned to the recipe. The original input, formats, and date object are also provided for context.
Usage
add_filter( 'automator_pro_formatter_format_date', 'your_function_name', 10, 5 );
Parameters
-
$output(mixed) - This parameter contains the formatted date string that will be returned by the hook.
-
$input(mixed) - This parameter holds the formatted date string that will be output by the function.
-
$from_format(mixed) - This parameter holds the original date value that needs to be formatted.
-
$to_format(mixed) - This parameter specifies the target date format for the output.
-
$date_object(mixed) - This parameter holds the `DateTime` object representing the input date, parsed according to the `$from_format`.
Return Value
The filtered value.
Examples
<?php
/**
* Modify the date output format if the date is in a specific timezone.
*
* This filter hook allows us to adjust the date formatting based on
* custom logic, in this case, checking if the date is within a specific
* timezone before applying the final format.
*
* @param mixed $output The formatted date string.
* @param mixed $input The original input date.
* @param mixed $from_format The original date format.
* @param mixed $to_format The target date format.
* @param DateTimeInterface|null $date_object The DateTime object, if available.
* @return mixed The potentially modified formatted date string.
*/
add_filter( 'automator_pro_formatter_format_date', function( $output, $input, $from_format, $to_format, $date_object ) {
// Check if we have a valid date object and if it's in a specific timezone
if ( $date_object instanceof DateTimeInterface && $date_object->getTimezone()->getName() === 'America/New_York' ) {
// If the date is in 'America/New_York', let's append something to indicate this
// and also reformat it to a slightly different structure for this specific timezone.
$nyc_specific_format = 'Y-m-d H:i:s (Eastern Time)';
$output = $date_object->format( $nyc_specific_format );
}
// Otherwise, return the original output generated by the plugin
return $output;
}, 10, 5 );
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:133
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 );
}