Skip to content

Instantly share code, notes, and snippets.

@tripflex
Created December 27, 2023 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tripflex/3838d7a156fa4d918ddc891cf8785226 to your computer and use it in GitHub Desktop.
Save tripflex/3838d7a156fa4d918ddc891cf8785226 to your computer and use it in GitHub Desktop.
Field editor format output using comma instead of period, and vice versa
<?php
add_filter('field_editor_get_custom_field_listing_meta', 'format_job_price_for_germany', 10, 4);
function format_job_price_for_germany($field_value, $field_slug, $listing_id, $args) {
// Check if the field slug is 'job_price'
if ($field_slug == 'job_price') {
// Ensure the field value is not empty and is a numeric value
if (!empty($field_value) && is_numeric($field_value)) {
// Format the number to German standards: comma for decimal and period for thousands
$field_value = number_format((float)$field_value, 2, ',', '.');
return $field_value;
}
}
// Return the original value if conditions are not met
return $field_value;
}
<?php
add_filter('field_editor_get_custom_field_listing_meta', 'format_job_price_for_germany', 10, 4);
function format_job_price_for_germany($field_value, $field_slug, $listing_id, $args) {
// Check if the field slug is 'job_price'
if ($field_slug == 'job_price') {
// Ensure the field value is not empty and is a numeric value
if (!empty($field_value) && is_numeric($field_value)) {
// Create a number formatter for German locale
$formatter = new NumberFormatter('de_DE', NumberFormatter::DECIMAL);
$field_value = $formatter->format($field_value);
return $field_value;
}
}
// Return the original value if conditions are not met
return $field_value;
}
@tripflex
Copy link
Author

The functions2.php version uses NumberFormatter which may or may not be installed on your hosting providers system

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment