Community Forums › Forums › Archived Forums › General Discussion › Adding javascript to admin
- This topic has 6 replies, 2 voices, and was last updated 9 years ago by marksalvatore.
-
AuthorPosts
-
April 19, 2015 at 12:35 pm #148451marksalvatoreMember
I'm trying to add a javascript file for use in the dashboard. It has one dependency, jquery.
I put the following in functions.php:function ms_include_admin_script() {
admin_enqueue_scripts( 'my-admin-script', get_template_directory_uri() . '/js/my-admin-script.js', array( 'jquery' ), null, true );
}
add_action( 'admin_enqueue_scripts', 'ms_include_admin_script' );This produces a white screen. I also tried wp_enqueue_scripts(). No white screen, but my script doesn't get loaded either.
April 19, 2015 at 3:58 pm #148463CraigMemberHey Mark, a couple of quick changes should get this working I think. You should definitely be using wp_enqueue_script within the function. If the theme is a child theme (which it likely is, I think) then get_template_directory_uri() is no good. This function will return the URL of the parent theme rather than the child. Try switching this for get_stylesheet_directory_uri() and see if it works.
function ms_include_admin_script() { wp_enqueue_script( ‘my-admin-script’, get_stylesheet_directory_uri() . ‘/js/my-admin-script.js’, array( ‘jquery’ ), null, true ); } add_action( ‘admin_enqueue_scripts’, ‘ms_include_admin_script’ );
April 19, 2015 at 4:21 pm #148467marksalvatoreMemberThanks Craig. I made the changes you suggested. How do I verify if it worked? I looked in the source for my javascript, but don't see it. Maybe it gets combined/compressed with the main script?
April 20, 2015 at 5:31 am #148511CraigMemberHi Mark, it should show up in the source in the footer, as that's where it is specified in wp_enqueue_script. There's no concatenation/ compression there which would hide it. I cut and pasted the code into my test site and was able to verify the code, the only thing I had to change was that the code paste used the wrong type of apostrophe so I changed them to ' instead of `.
April 20, 2015 at 8:48 am #148536marksalvatoreMemberhmm.. Looking at the comma-delimited list of js that's loading in the footer, I see a long list of filenames, but none is the one I'm trying to load. But that's fine. At least I know the call is correct. Thanks for that. I should be able to figure it out from here.
By the way, why are you suggesting I use wp_enqueue_scripts() rather than admin_enqueue_scripts()? I only want it to load in the admin.
April 20, 2015 at 9:02 am #148539CraigMemberHi Mark, I haven't suggested that you use wp_enqueue_scripts(). I have stated that inside your function ms_include_admin_script() you need to use wp_enqueue_script() to call your .js file. Your function ms_include_admin_script() is then hooked to admin_enqueue_scripts.
https://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
April 20, 2015 at 9:25 am #148545marksalvatoreMemberOh, got it now! I had it correct in the post, but not in my code. Thanks for keeping with me!
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.