admin管理员组

文章数量:1287160

I am using the headers and footers plugin to call a small script I made by pasting the script directly into the user interface of the plugin. However, I would like to upload a JS file and call that instead.

Where should I upload the file? My guess would be the child theme.

I am using the headers and footers plugin to call a small script I made by pasting the script directly into the user interface of the plugin. However, I would like to upload a JS file and call that instead.

Where should I upload the file? My guess would be the child theme.

Share Improve this question asked Apr 23, 2019 at 14:53 mso4491mso4491 11 silver badge1 bronze badge 2
  • Possible duplicate of Proper Javascript Implementation – MikeNGarrett Commented Apr 23, 2019 at 15:09
  • Uploading JS to the media library isn't supported, you're going to have to upload the file to the server itself and modify the PHP code to load it – Tom J Nowell Commented Apr 23, 2019 at 15:31
Add a comment  | 

1 Answer 1

Reset to default 2

Yes. You will want to enqueue your scripts using wp_enqueue_scripts and then upload them into your child theme. I normally create a /js/ directory to keep things organized.

Here is a sample of how to enqueue a file inside of a themes JS subfolder.

function wpse_enqueue_scripts() {    

    wp_enqueue_script(
        'js-functions',
        get_stylesheet_directory_uri() . '/js/functions.js', array('jquery'), filemtime(get_stylesheet_directory() . '/js/functions.js')
    );

}
add_action('wp_enqueue_scripts', 'wpse_enqueue_scripts', 999);

本文标签: Where to upload JavaScript file in WordPress