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 )

functions.php

<?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>";
}
No comments yet.

Leave a Reply