This doesnt work for Plugin get_template_directory_uri()

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

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'
);

Leave a Comment