function Zumper_widget_enqueue_script()
add_action('admin_print_scripts-widgets.php', 'Zumper_widget_enqueue_script');

Suppose a javascript is sitting in my wordpress theme in this path- admin/js/sample.js

and the theme is using wp_enqueue_script to include this file -


Now my question is if I am transferring the widget and javascript to plugin- widget codes will go in plugin.php file and javascript file will go into this path -


I am enqueing it like this -


but its not getting included.

Is this function unable to pull the correct path - get_template_directory_uri() when used in plugin folder?

1 Answer 1


get_template_directory_uri works in a plugin, in that it returns the active theme directory URI, which is what the template directory is.

If you're trying to get the URI for your plugin assets, then you want plugin_dir_url.

    plugin_dir_url( __FILE__ ) . 'js/sample.js'
  • Thanks and the same is true for if we wish to include a CSS File or there is some alternative way to do this?
    – WordCent
    Feb 5, 2016 at 19:34
  • you would use it any time you need the URI of your plugin's directory- for js files, css files, images, etc..
    – Milo
    Feb 5, 2016 at 19:57
  • Something is still missing - I used this to inlcude javascript added in js folder of plugin director, but it doesn't work. function Zumper_widget_enqueue_script() { wp_enqueue_script( 'my_custom_script', plugin_dir_url( FILE ) . 'js/jquery.repeatable.js' ); } add_action('admin_enqueue_scripts', 'Zumper_widget_enqueue_script'); I did some research and found this - codex.wordpress.org/Function_Reference/plugin_dir_url it seems that they are registering it first that is about CSS. Can you please guide me what is the correct approach so that my javascript files get included?
    – WordCent
    Feb 6, 2016 at 12:02
  • The function has nothing to do with which type of file you are adding, it returns a URL, that is all. Edit your question with the code you are using, code in comments is unreadable. admin_enqueue_scripts hook is for adding scripts to admin screens, which screen are you trying to use your script on?
    – Milo
    Feb 6, 2016 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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