2
function Zumper_widget_enqueue_script()
{
    wp_enqueue_script('jquery-repeater-form',get_template_directory_uri().'/admin/js/sample.js');
}
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 -

wp_enqueue_script('jquery-repeater-form',get_template_directory_uri().'/admin/js/sample.js');

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 -

js/sample.js

I am enqueing it like this -

wp_enqueue_script('jquery-repeater-form',get_template_directory_uri().'/js/sample.js');

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

3

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.

wp_enqueue_script(
    'jquery-repeater-form',
    plugin_dir_url( __FILE__ ) . 'js/sample.js'
);
4
  • 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
    Commented 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
    Commented 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
    Commented 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
    Commented Feb 6, 2016 at 16:54

Your Answer

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

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