• viki

    (@sheoran8210)


    Hello folks,

    I have written some javascript code for something. What I want to know is, In which directory should I upload this file and in which file header should I embed javascript file name, So that I can use this code globally. I guess I will have to write in my theme file code but I don’t know which one. Can someone assist me please?

    Thanks all.

Viewing 1 replies (of 1 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with wp_enqueue_script() in your theme’s functions.php.

    https://codex.www.ads-software.com/Function_Reference/wp_enqueue_script

    Create a “js” folder inside your theme and put your javascript file inside it. Then use something simular to this in your theme’s functions.php:

    function my_javascripts_files() {
    
      // file to enqueue: my_javascript.js
    
      // directory: wp-content/themes/yourtheme/js/
    
      wp_enqueue_script('my_script', get_template_directory_uri() . '/js/my_javascript.js');
    }
    add_action('wp_enqueue_scripts', 'my_javascripts_files');

Viewing 1 replies (of 1 total)
  • The topic ‘Where should I add a javascript file’ is closed to new replies.