Skip to main content
Bumped by Community user
deleted 119 characters in body; edited title
Source Link
Gabriel
  • 2.2k
  • 10
  • 22
  • 24

How to add JavaScript file to Wordpress using wp_enqueue_scripts?

I know PHP, HTML, CSS and JavaScript but I'm kind of new when it comes to wordpress and digging in the source code.

How ever I have made a very simple JavaScript Scriptscript. There is 1 .js.js file with alota lot of code, and in the html-fileHTML file there is only 2 lines. The script works fine when I test it on a plain htmlHTML page. But, but when I try it with wordpressWordPress, it does'ntdoesn't work.

I guess it has something to do with the path to the external .js file ((src="js/file.js")).

What am I missing? Why is'ntisn't my script loading correctly?

Edit: See comment to answer below, I have now added the code to the function.phpfunctions.php, but still not working.

Edit2: This is the code I use in the function.phpfunctions.php:

add_action( 'wp_enqueue_scripts', 'my_enqueue_front_scripts' );
function my_enqueue_front_scripts(){
    wp_enqueue_script( 'my-custom-scripts', get_stylesheet_directory_uri() . '/js/file.js' );
}

And this is the code in my head tag:

<script type="text/javascript"> url_detect.OnBack = function() { window.location.replace("URL.org"); } </script>

How to add JavaScript file to Wordpress using wp_enqueue_scripts?

I know PHP, HTML, CSS and JavaScript but I'm kind of new when it comes to wordpress and digging in the source code.

How ever I have made a very simple JavaScript Script. There is 1 .js file with alot of code, and in the html-file there is only 2 lines. The script works fine when I test it on a plain html page. But when I try it with wordpress it does'nt work.

I guess it has something to do with the path to the external .js file (src="js/file.js").

What am I missing? Why is'nt my script loading correctly?

Edit: See comment to answer below, I have now added the code to the function.php, but still not working.

Edit2: This is the code I use in the function.php

add_action('wp_enqueue_scripts', 'my_enqueue_front_scripts');
function my_enqueue_front_scripts(){
    wp_enqueue_script('my-custom-scripts', get_stylesheet_directory_uri().'/js/file.js');
}

And this is the code in my head tag:

<script type="text/javascript"> url_detect.OnBack = function() { window.location.replace("URL.org"); } </script>

How to add JavaScript file using wp_enqueue_scripts?

I have made a very simple JavaScript script. There is 1 .js file with a lot of code, and in the HTML file there is only 2 lines. The script works fine when I test it on a plain HTML page, but when I try it with WordPress, it doesn't work.

I guess it has something to do with the path to the external .js file (src="js/file.js").

What am I missing? Why isn't my script loading correctly?

Edit: See comment to answer below, I have now added the code to the functions.php, but still not working.

Edit2: This is the code I use in the functions.php:

add_action( 'wp_enqueue_scripts', 'my_enqueue_front_scripts' );
function my_enqueue_front_scripts(){
    wp_enqueue_script( 'my-custom-scripts', get_stylesheet_directory_uri() . '/js/file.js' );
}

And this is the code in my head tag:

<script type="text/javascript"> url_detect.OnBack = function() { window.location.replace("URL.org"); } </script>
Title refined to convey the question goal,also added one tag
Source Link

Why is my script not working How to add JavaScript file to Wordpress using wp_enqueue_scripts?

I know PHP, HTML, CSS and JavaScript but I'm kind of new when it comes to wordpress and digging in the source code.

How ever I have made a very simple JavaScript Script. There is 1 .js file with alot of code, and in the html-file there is only 2 lines. The script works fine when I test it on a plain html page. But when I try it with wordpress it does'nt work.

I guess it has something to do with the path to the external .js file (src="js/file.js").

What am I missing? Why is'nt my script loading correctly?

Edit: See comment to answer below, I have now added the code to the function.php, but still not working.

Edit2: This is the code I use in the function.php

add_action('wp_enqueue_scripts', 'my_enqueue_front_scripts');
 function my_enqueue_front_scripts(){
    wp_enqueue_script('my-custom-scripts', get_stylesheet_directory_uri().'/js/file.js');
}

And this is the code in my head tag:

<script type="text/javascript"> url_detect.OnBack = function() { window.location.replace("URL.org"); } </script>

Why is my script not working?

I know PHP, HTML, CSS and JavaScript but I'm kind of new when it comes to wordpress and digging in the source code.

How ever I have made a very simple JavaScript Script. There is 1 .js file with alot of code, and in the html-file there is only 2 lines. The script works fine when I test it on a plain html page. But when I try it with wordpress it does'nt work.

I guess it has something to do with the path to the external .js file (src="js/file.js").

What am I missing? Why is'nt my script loading correctly?

Edit: See comment to answer below, I have now added the code to the function.php, but still not working.

Edit2: This is the code I use in the function.php

add_action('wp_enqueue_scripts', 'my_enqueue_front_scripts');
 function my_enqueue_front_scripts(){
  wp_enqueue_script('my-custom-scripts', get_stylesheet_directory_uri().'/js/file.js');
}

And this is the code in my head tag:

<script type="text/javascript"> url_detect.OnBack = function() { window.location.replace("URL.org"); } </script>

How to add JavaScript file to Wordpress using wp_enqueue_scripts?

I know PHP, HTML, CSS and JavaScript but I'm kind of new when it comes to wordpress and digging in the source code.

How ever I have made a very simple JavaScript Script. There is 1 .js file with alot of code, and in the html-file there is only 2 lines. The script works fine when I test it on a plain html page. But when I try it with wordpress it does'nt work.

I guess it has something to do with the path to the external .js file (src="js/file.js").

What am I missing? Why is'nt my script loading correctly?

Edit: See comment to answer below, I have now added the code to the function.php, but still not working.

Edit2: This is the code I use in the function.php

add_action('wp_enqueue_scripts', 'my_enqueue_front_scripts');
function my_enqueue_front_scripts(){
    wp_enqueue_script('my-custom-scripts', get_stylesheet_directory_uri().'/js/file.js');
}

And this is the code in my head tag:

<script type="text/javascript"> url_detect.OnBack = function() { window.location.replace("URL.org"); } </script>

I know PHP, HTML, CSS and JavaScript but I'm kind of new when it comes to wordpress and digging in the source code.

How ever I have made a very simple JavaScript Script. There is 1 .js file with alot of code, and in the html-file there is only 2 lines. The script works fine when I test it on a plain html page. But when I try it with wordpress it does'nt work.

I guess it has something to do with the path to the external .js.js file (src="js/file.js")(src="js/file.js").

What am I missing? Why is'nt my script loading correctly?

Edit: See comment to answer below, I have now added the code to the function.php, but still not working.

Edit2: This is the code I use in the function.phpfunction.php

add_action('wp_enqueue_scripts', 'my_enqueue_front_scripts');

function my_enqueue_front_scripts(){

 function my_enqueue_front_scripts(){
  wp_enqueue_script('my-custom-scripts', get_stylesheet_directory_uri().'/js/file.js');
}

}

And this is the code in my head tag:

<script type="text/javascript"> url_detect.OnBack = function() { window.location.replace("URL.org"); } </script>

I know PHP, HTML, CSS and JavaScript but I'm kind of new when it comes to wordpress and digging in the source code.

How ever I have made a very simple JavaScript Script. There is 1 .js file with alot of code, and in the html-file there is only 2 lines. The script works fine when I test it on a plain html page. But when I try it with wordpress it does'nt work.

I guess it has something to do with the path to the external .js file (src="js/file.js").

What am I missing? Why is'nt my script loading correctly?

Edit: See comment to answer below, I have now added the code to the function.php, but still not working.

Edit2: This is the code I use in the function.php

add_action('wp_enqueue_scripts', 'my_enqueue_front_scripts');

function my_enqueue_front_scripts(){

wp_enqueue_script('my-custom-scripts', get_stylesheet_directory_uri().'/js/file.js');

}

And this is the code in my head tag:

<script type="text/javascript"> url_detect.OnBack = function() { window.location.replace("URL.org"); } </script>

I know PHP, HTML, CSS and JavaScript but I'm kind of new when it comes to wordpress and digging in the source code.

How ever I have made a very simple JavaScript Script. There is 1 .js file with alot of code, and in the html-file there is only 2 lines. The script works fine when I test it on a plain html page. But when I try it with wordpress it does'nt work.

I guess it has something to do with the path to the external .js file (src="js/file.js").

What am I missing? Why is'nt my script loading correctly?

Edit: See comment to answer below, I have now added the code to the function.php, but still not working.

Edit2: This is the code I use in the function.php

add_action('wp_enqueue_scripts', 'my_enqueue_front_scripts');
 function my_enqueue_front_scripts(){
  wp_enqueue_script('my-custom-scripts', get_stylesheet_directory_uri().'/js/file.js');
}

And this is the code in my head tag:

<script type="text/javascript"> url_detect.OnBack = function() { window.location.replace("URL.org"); } </script>
added 261 characters in body
Source Link
Alex
  • 375
  • 7
  • 18
Loading
added 108 characters in body
Source Link
Alex
  • 375
  • 7
  • 18
Loading
Source Link
Alex
  • 375
  • 7
  • 18
Loading