0

Here is how I am enqueueing the script (this is inside a class):

add_action( 'admin_enqueue_scripts', array( $this, 'add_admin_scripts'), true);

public function add_admin_scripts(){
    $parentBase = get_current_screen();
    if($parentBase->id == 'toplevel_page_calc-settings'){
            wp_enqueue_style( 'l7w-admin-js', $this->pluginUrl . 'assets/js/admin.min.js');
    }
}

Here is the simple script:

$(document).ready(function(){
    alert("Works");
    console.log('Works');
});

The js is showing and is loaded on the settings page but the alert() and the console.log() are not executed.

Why am I not seeing the alert or the console.log()?

3
  • You should see an error in your console related to the $, WordPress' jQuery object reference is jQuery, not $. – Milo Sep 8 '17 at 20:20
  • I am seeing this warning: Resource interpreted as Stylesheet but transferred with MIME type application/javascript – Jeff Mattson Sep 8 '17 at 20:23
  • 1
    oh, you're using wp_enqueue_style instead of wp_enqueue_script. – Milo Sep 8 '17 at 20:25
0

Got it:

wp_enqueue_style( 'l7w-admin-js', $this->pluginUrl . 'assets/js/admin.min.js');

wp_enqueue_script( 'l7w-admin-js', $this->pluginUrl . 'assets/js/admin.min.js');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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