Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
How are you wanting this to work? Are you talking about dynamic reformatting as the user types? –  s_ha_dum Jan 21 '13 at 22:46
    
yes exactly. I know some jquery plugins like the one simon showed me below, but i am not sucessfully enqueing it in admin area –  user1576978 Jan 22 '13 at 20:19
    
Post the code you are trying to use to enqueue the script. –  s_ha_dum Jan 22 '13 at 20:24
    
there's it, in my edit of the question –  user1576978 Jan 22 '13 at 21:08

3 Answers 3

up vote 0 down vote accepted

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

share|improve this answer
    
not really it would be more like a jquery because i need it to format the currency only on admin page while the user insert them and then i need to store the number as INT on the DB so i can sort by custom meta with no problems, thank you –  user1576978 Jan 21 '13 at 23:26
    
ah, i understand - i've expanded my answer to include jQuery autoNumeric which hopefully does what you need –  Simon Blackbourn Jan 21 '13 at 23:36
    
Simon, thats basically what I need, but i'm having some trouble in enqueueing the script on admin area! if you could please please pleeeease help me with that i'd be very gratefull. Note: i'm trully looking for it. I understand I need admin_enqueue_scripts but not sure how to use it in a hook –  user1576978 Jan 22 '13 at 20:11
    
it looks like @s_ha_dum has answered this below, it's THEME_DIR that's causing the problem. –  Simon Blackbourn Jan 22 '13 at 23:41

Autonumeric is a jQuery plugin and looks to be written so that you don't have to swap out the $.

I think the problem is this line:

wp_register_script( 'autonum', THEME_DIR . '/js/vendor/autoNumeric.js', array( 'jquery' ), '1', false ); //this is autoNumeric for currency format

The problem is that I am not sure where THEME_DIR is coming from. That constant is not defined on my test install. I suspect that you have a broken URL for that script.

At any rate, even if THEME_DIR is defined and I just missed it, instead of that constant you should probably be using get_template_directory_uri() if this is a standalone, or parent, theme or get_stylesheet_directory_uri() if it is a child theme.

share|improve this answer

I think is better, that you add with a plugin a meta box for your price field. There is easy to filter and format the output. The sore inside the database and it is easier to maintance or add the values inside the field for authors. For read, get output the value can you also use the default functions of custom post type. It give many tutorials and answers on WPSE to this topic.

For the format with php it is easy, if you use the money_format() function.

  • A example: echo money_format('$%i', 3.4); // echos '$3.40'

I think this function is the right way. The function use the locale var from the WordPress install and and works with different types and languages. A format with the default php functions is not usable for longer time, is the hard way to format in this context.

If you like a way in javascript, then see on this WPSE thread, very helpfull. But I think, the php way is fast and easy for a output inside a custom field, like in a Meta box.

share|improve this answer
    
thank you for awnser, but my problem is that i can add a meta with INT value, but i need to make it format the currency for the user to see it, and save it as integer –  user1576978 Jan 21 '13 at 23:19
    
See my update on this answer. Many languages have a function for this job, also php. –  bueltge Jan 23 '13 at 11:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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