Skip to content

Instantly share code, notes, and snippets.

@tripflex
Last active September 14, 2020 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tripflex/d49cc84fc05c77cf9662 to your computer and use it in GitHub Desktop.
Save tripflex/d49cc84fc05c77cf9662 to your computer and use it in GitHub Desktop.
How to turn a WP Job Manager Field Editor standard text field into a currency field with jQuery Mask Money ( https://github.com/plentz/jquery-maskmoney )
<?php
// ^ the <?php above should only be in your functions.php file ONCE, at the top ... do not include <?php if you're adding this to the end of your functions.php file
// Register our jQuery MaskMoney script in WordPress (does not output, only register)
add_action( 'wp_enqueue_scripts', 'my_custom_register_currency_script' );
// Call our function to output jQuery when the submit listing page is loaded
add_action( 'submit_job_form_job_fields_end', 'my_custom_output_currency_js' );
function my_custom_register_currency_script(){
// Register the script in WP .. does not output, only register
wp_register_script( 'custom-currency-field', '//cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js', array( 'jquery' ) );
}
function my_custom_output_currency_js(){
// Output our script registered above only on the Job Submit page
wp_enqueue_script( 'custom-currency-field' );
// Output custom jQuery to register and initialize the field with our custom configuration values
// ... make sure to change #YOUR_META_KEY to whatever the meta key is for your field, so if your meta key is
// job_salary, then it should be #job_salary
// See this link for all the available options (thousands, decimal, allowZero, etc):
// https://github.com/plentz/jquery-maskmoney
echo "<script>jQuery(function($){ $( '#YOUR_META_KEY' ).maskMoney({thousands:'', decimal:'.', allowZero:true, suffix: '€'}); });</script>";
// If you want to do this for multiple fields (meta keys), then all you need to do is copy this part:
// $( '#YOUR_META_KEY' ).maskMoney({thousands:'', decimal:'.', allowZero:true, suffix: '€'});
// and place it after the first semicolon. So as an example, this would be the code for two fields:
// echo "<script>jQuery(function($){ $( '#YOUR_META_KEY' ).maskMoney({thousands:'', decimal:'.', allowZero:true, suffix: '€'}); $( '#job_salary' ).maskMoney({thousands:'', decimal:'.', allowZero:true, suffix: '€'}); });</script>";
}
@tripflex
Copy link
Author

tripflex commented Jan 8, 2018

Combine this with this code snippet to output value as formatted currency (if you're not using the suffix or prefix configurations):
https://gist.github.com/tripflex/849295c2c2a59a92585a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment