Skip to content

Instantly share code, notes, and snippets.

@tripflex
Last active September 14, 2020 19:00
Show Gist options
  • Select an option

  • Save tripflex/d49cc84fc05c77cf9662 to your computer and use it in GitHub Desktop.

Select an option

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

tripflex commented Jan 8, 2018

Copy link
Copy Markdown
Author

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