I know that there already are several topics with this matter but none of them seem to resolve my problem.
I want to link in a custom WordPress theme that I am creating several JavaScript files. I’ve tried a lot of possible ways and none of them seem to work for me.
To skip all the ways I’ve already tried to add the files please tell me for example how can I add the file “prettyPhoto.js” in order to work in my custom theme.
Thank you,
Andi
The right way is to use
wp_register_script
andwp_enqueue_script
. Using these will instruct WordPress to load your script after any dependent scripts.So say your
prettyPhoto.js
file is inmytheme/js
. Infunctions.php
, add something like:If
prettyPhoto.js
is dependent on jQuery or something, then you specify this inwp_register_script
. Doing the below will tell WordPress to ensure jQuery is loaded before your script is loaded.See wp_register_script and wp_enqueue_script in the codex for more.
This article will probably help, too.
I guess it didn’t work for You, because You probably forgot about placing wp_head()
or about placing wp_footer()
Take some time and make yourself familiar with the Theme Development Checklist.
It’s hard to guess without seeing the template and without knowing what You’ve tried already,
but what You need is the wp_enqueue_script function (in
functions.php
) together with wp_head() and|or wp_footer() (in the template).wp_head()
right before</head>
in the template.wp_footer()
right before</body>
in the template.Place the following code in
functions.php
: