Created
December 27, 2023 21:00
-
-
Save tripflex/3838d7a156fa4d918ddc891cf8785226 to your computer and use it in GitHub Desktop.
Field editor format output using comma instead of period, and vice versa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
functions2.php
version uses NumberFormatter which may or may not be installed on your hosting providers system