WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am attempting to implement a javascript function within a page. It is implemented correctly in the header and loads correctly:

<?php function forceNumber2() {
    wp_enqueue_script( 'forceNumber2',  get_template_directory_uri() . '/js/forceNumber2.js', array( 'jquery' ) );
add_action( 'wp_enqueue_scripts', 'forceNumber2' );

function numberWithCommas2() {
    wp_enqueue_script( 'numberWithCommas2', get_template_directory_uri() . '/js/numberWithCommas2.js', array( 'jquery' )    );
add_action( 'wp_enqueue_scripts', 'numberWithCommas2' );

function addDashes2() {
    wp_enqueue_script( 'addDashes2',    get_template_directory_uri() . '/js/addDashes.js', array( 'jquery' )    );
add_action( 'wp_enqueue_scripts', 'addDashes2' );?>

However when I implement the call to the JS I get an error in the console with each keystroke "ReferenceError: forceNumber2 is not defined" and "ReferenceError: numberWithCommas2 is not defined"


<form method="post" id="adduser" action="<?php the_permalink(); ?>">
                    <p class='form-username'>
                        <label for="first-name" style='margin-left:0px;display:block' >Update Yearly Income</label>
                        <span style='font-size:16px;font-weight:bold'>&#36;</span>
                        <input class="input" name="first-name" type="text" id="first-name" size="25" value="<?php the_author_meta( 'yearly_income', $current_user->ID ); ?>" style='width:198px; text-align:right' onkeypress="return forceNumber2(event);" onkeyup="this.value=numberWithCommas2(this.value);"
                    <span style='font-size:16px;font-weight:bold'>per year</span>
                    <p class="form-submit">
                        <?php echo $referer; ?>
                        <input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e('Update', 'profile'); ?>" />
                        <?php wp_nonce_field( 'update-user' ) ?>
                        <input name="action" type="hidden" id="action" value="update-user" />
                    </p><!-- .form-submit -->
                </form><!-- #adduser -->

The console is unclear as to why the function is not defined but yet is loaded correctly in the header.

share|improve this question
Is there a reason you're not enqueueing them all from the same function rather than three separate? – hereswhatidid Jun 22 '13 at 18:28
I assume that forceNumber2 is a function defined in forceNumber2.js? And likewise with numberWithCommas2/numberWithCommas22.js? – s_ha_dum Jun 22 '13 at 18:34
forceNumber2.js reads function forceNumber2(e) { var keyCode = e.keyCode ? e.keyCode : e.which; if((keyCode < 48 || keyCode > 58) && keyCode != 8 && keyCode != 9 && keyCode != 46 && keyCode != 188 && keyCode != 189) { return false; } return true; } – Helena Jun 22 '13 at 18:35
There is no particular reason I am enqueueing them seperately other than my own ease of use. – Helena Jun 22 '13 at 18:35
@Helena You have a funny idea of ease of use ;) – TheDeadMedic Jun 22 '13 at 23:57

Your Answer


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

Browse other questions tagged or ask your own question.