1
wp_register_script('SliderViewer', '/js/SliderViewer-1.2.js');
wp_enqueue_script('SliderViewer');

get_header();

however, mydomain.com/js/SliderViewer-1.2.js doesn't exist.

My js is in the ftp at

thewebsite -> wp-content -> themes -> BLANK-Theme -> js

What's the path of that? Thanks-

1 Answer 1

3

You're registering/enqueueing your script wrong. You should register/enqueue in your theme's functions.php file instead of inside the header/page.

Also, you need to use your theme's directory ... which will be along the lines of mydomain.com/wp-content/themes/BLANK-Theme/js/SliderViewer-1.2.js.

Use this code in functions.php:

function my_scripts_enqueue_method() {
    wp_enqueue_script(
        'SliderViewer',
        get_template_directory_uri() . '/js/SliderViewer-1.2.js'
    );
}
add_action( 'wp_enqueue_scripts', 'my_scripts_enqueue_method' );

This will enqueue and register your script, and ensure that you're using the appropriate directory for your theme in the process.

6
  • I'm just trying to do this for ONE page though.
    – Jacksonkr
    Apr 9, 2012 at 21:30
  • 1
    For one page, then, you should really be using a custom page template. But the guidance on referencing the script in your call to wp_enqueue_script() stands.
    – EAMann
    Apr 9, 2012 at 21:33
  • This all works (accepted), but now I'm curious.. What was the problem with using wp_register_script? I like to be enlightened.
    – Jacksonkr
    Apr 9, 2012 at 21:37
  • 1
    No problem, just unnecessary. wp_enqueue_script() will automatically register the script for you. The biggest change was using get_template_directory_uri() to build the path to your script.
    – EAMann
    Apr 9, 2012 at 21:39
  • but you register with this snippet on all pages, backend and frontend. Use Conditional Tags, like is_admin() and is_singular() to load only on page, there you will use this script.
    – bueltge
    Apr 10, 2012 at 7:16

Your Answer

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

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