-1

I am working on a Wordpress website for a client to show several property listings and their associated prices. When I enter the number in the metabox created by the developer it strips out all decimals. So that if I enter 16.50 it immediately turns into 1650 when the page is saved. 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/Wordpress for it to make sense. The developer directed me to adjusting code for formatting it however that simply adds 2 decimals onto the number to show 1650.00 and not 16.50 like I want. The decimal is immediately stripped upon entering it in the metabox. I feel as though somewhere there is something defined that strips out all decimals from this entry field but I'm not sure how to counteract it.

Here is what happens in the backend:

https://cl.ly/2u341p2g0i3p

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.

**** Edit ****

Here is the code I found for update_post_meta:

if($post_id) {

            // Update Custom Meta
            update_post_meta($post_id, '_ct_listing_alt_title', esc_attr(strip_tags($_POST['customMetaAltTitle'])));
            update_post_meta($post_id, '_ct_price', esc_attr(strip_tags($_POST['customMetaPrice'])));
            update_post_meta($post_id, '_ct_price_prefix', esc_attr(strip_tags($_POST['customMetaPricePrefix'])));
            update_post_meta($post_id, '_ct_price_postfix', esc_attr(strip_tags($_POST['customMetaPricePostfix'])));
            update_post_meta($post_id, '_ct_sqft', esc_attr(strip_tags($_POST['customMetaSqFt'])));
            update_post_meta($post_id, '_ct_video', esc_attr(strip_tags($_POST['customMetaVideoURL'])));
            update_post_meta($post_id, '_ct_mls', esc_attr(strip_tags($_POST['customMetaMLS'])));
            update_post_meta($post_id, '_ct_rental_guests', esc_attr(strip_tags($_POST['customMetaMaxGuests'])));
            update_post_meta($post_id, '_ct_rental_min_stay', esc_attr(strip_tags($_POST['customMetaMinStay'])));
            update_post_meta($post_id, '_ct_rental_checkin', esc_attr(strip_tags($_POST['customMetaCheckIn'])));
            update_post_meta($post_id, '_ct_rental_checkout', esc_attr(strip_tags($_POST['customMetaCheckOut'])));
            update_post_meta($post_id, '_ct_rental_extra_people', esc_attr(strip_tags($_POST['customMetaExtraPerson'])));
            update_post_meta($post_id, '_ct_rental_cleaning', esc_attr(strip_tags($_POST['customMetaCleaningFee'])));
            update_post_meta($post_id, '_ct_rental_cancellation', esc_attr(strip_tags($_POST['customMetaCancellationFee'])));
            update_post_meta($post_id, '_ct_rental_deposit', esc_attr(strip_tags($_POST['customMetaSecurityDeposit'])));

            //Update Custom Taxonomies
            wp_set_post_terms($post_id,array($_POST['ct_property_type']),'property_type',true);
            wp_set_post_terms($post_id,array($_POST['customTaxBeds']),'beds',true);
            wp_set_post_terms($post_id,array($_POST['customTaxBaths']),'baths',true);
            wp_set_post_terms($post_id,array($_POST['ct_ct_status']),'ct_status',true);
            wp_set_post_terms($post_id,array($_POST['customTaxCity']),'city',true);
            wp_set_post_terms($post_id,array($_POST['customTaxState']),'state',true);
            wp_set_post_terms($post_id,array($_POST['customTaxZip']),'zipcode',true);
            wp_set_post_terms($post_id,array($_POST['customTaxFeat']),'additional_features',true);

            // Redirect
            wp_redirect( home_url() . '/?page_id=' . $view ); exit;
        }
1
  • sorry, but this is not a code review/debugging service, it is just not possible to debug things for you by just looking at code unless it is a glaring error. In addition this is only part of the code which might be the cause for the error. Commented May 14, 2017 at 3:26

1 Answer 1

-1

So you want to be doing something like this instead...

$price = number_format($price_meta, 2);
echo $price;

The significant part here is the 2, it will add the 2 zeros after the . if you wanted it to not show the zeros you would set this as 0 - No need to be adding in dots and what not yourself when it is done for you!

Hope that helps :)

***** UPDATE *****

So after you updated your code to show where / how it is being updated, it is not any clearer unfortunately. I want to agree with @Mark, there are a lot of things along the way that could be causing this.

You also removed the first bit of code which wasn't too helpful :/

From looking at that, plus what you've added, I would start by removing this;

$price_meta= preg_replace('/[\$,]/', '', $price_meta);

You are updating correctly by the looks of it using;

update_post_meta($post_id, '_ct_price', esc_attr(strip_tags($_POST['customMetaPrice'])));

You should be echoing it the way I showed above;

$price = number_format($price_meta, 2);
echo $price;

I have just tested this out and it works perfectly, I am also doing it on my own system using AJAX, it also works perfectly like that too.

Check it here - https://cl.ly/0u2v2P1a112t

Without more detail it will be difficult to debug.

6
  • Thank you for your post however this does not appear to solve my issue. Please reference the link I posted as a revision to show how the decimal gets stripped from the metabox.
    – Kristin P
    Commented May 14, 2017 at 2:12
  • Ok. So can you show the code for how you are saving that price. Because that is where your issue is. Not in the way it is displayed. The way I showed you how to display it is correct. The problem is definitely in the saving of the data. Post the code that has your update_post_meta() Commented May 14, 2017 at 3:46
  • Thank you! Please see updated post for the update_post_meta() code @jay.
    – Kristin P
    Commented May 14, 2017 at 15:22
  • Thank you for the update. I did see your example works well so there must be something else causing me to still having issues. I just don't know where to look really since I didn't develop the theme nor am I developer, but i appreciate your time.
    – Kristin P
    Commented May 15, 2017 at 19:21
  • I see. What theme is it? I have worked with alot of property ones :) Commented May 15, 2017 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.