Currency (price) formating on custom fields

I need to format a custom field in admin area (post edit) as a currency, eq: 12.000,00 , with user typing only the numbers, the , and . automatic. Ive searched arround, with no sucess.. Anyone? thank

The code i am trying to use to enqueue script in admin is

function enqueue_admin() {

    wp_enqueue_script( 'jquery' ); //adding Jquery to admin. Not sure if is needed or already there
    wp_register_script( 'autonum', THEME_DIR . '/js/vendor/autoNumeric.js', array( 'jquery' ), '1', false ); //this is autoNumeric for currency format
    wp_enqueue_script( 'autonum' );

}
add_action( 'admin_enqueue_scripts', 'enqueue_admin' );

Also, not sure if i need to change all the $ to JQuery in autoNumeric.js, because of wordpress compatibility

4 Answers
4

if you want to process the field after submitting, php’s number_format function is what you need:

$price = number_format( $number, 2, '.', ',' );

but if you want to change how the number is displayed as it’s typed in, try jQuery autoNumeric

Leave a Comment