I’m trying to modify a bit of PHP in WordPress theme I did not create and the developer is not very helpful.

I am working on a website for a client to show several property listings and their associated prices. When I enter the number in the editor it strips out all decimals. So that if I enter 16.50 it immediately turns into 1650. I want it to be able to show 2 decimals. I’ve done a bit of research but I’m not knowledgeable enough in PHP for it to make sense. The developer directed me to adjusting the following code for $price_meta however that simply adds 2 decimals onto the number to show 1650.00. It seems like the decimal is immediately stripped upon entering it. Is there something else I need to be doing to get it to show correctly?

if(!function_exists('ct_listing_price')) {
function ct_listing_price() {
    global $post;
    global $ct_options;

    $ct_currency_placement = $ct_options['ct_currency_placement'];

    $price_prefix = get_post_meta(get_the_ID(), '_ct_price_prefix', true);
    $price_postfix = get_post_meta(get_the_ID(), '_ct_price_postfix', true);

    $price_meta = get_post_meta(get_the_ID(), '_ct_price', true);
    $price_meta= preg_replace('/[$,]/', '', $price_meta);

    if($ct_currency_placement == 'after') {
        if(!empty($price_prefix)) {
            echo "<span class='listing-price-prefix'>";
                echo esc_html($price_prefix) . ' ';
            echo '</span>';
        }
        if(!empty($price_meta)) {
            echo "<span class='listing-price'>";
                echo number_format($price_meta, 2);
                ct_currency();
            echo '</span>';
        }
        if(!empty($price_postfix)) {
            echo "<span class='listing-price-postfix'>";
                echo  ' ' . esc_html($price_postfix) . ' ';
            echo '</span>';
        }
    } else {
        if(!empty($price_prefix)) {
            echo esc_html($price_prefix) . ' ';
        }
        if(!empty($price_meta)) {
            echo "<span class='listing-price'>";
                ct_currency();
                echo number_format($price_meta, 2);
            echo '</span>';
        }
        if(!empty($price_postfix)) {
            echo  ' ' . esc_html($price_postfix);
        }
    }
}

I have very limited knowledge of PHP so if you have an idea how to help please explain in as simple and prescriptive terms as possible.

Read more here: Number formatting incorrectly in PHP


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: