2

I have a website that uses WordPress for its blog (the website itself is not WordPress). So the structure is:

 www.website.com (not WordPress)
 www.website.com/blog (WordPress)

I have a JavaScript file that is used for both the website and the blog. But when I link to the file in the functions.php file, WordPress tries to find it in the theme:

// Register the script
wp_register_script('circle-bounce', get_template_directory_uri() . '/js/general.js', array( 'jquery', 'jquery-effects-core', 'jquery-effects-bounce'));

This looks for the general.js file in the folder for the wordpress child theme. In the source, it links to:

http://www.website.com/blog/wp-content/themes/twentytwelve/js/general.js?ver=4.2.2

I actually need it to get the file inside the js folder in the root of the website. So the file can actually be found at:

website.com/js/general.js

The only way I've managed to achieve this is to change the registration to:

// Register the script
wp_register_script('circle-bounce', 'http://www.website.com/js/general.js', array( 'jquery', 'jquery-effects-core', 'jquery-effects-bounce'));

I'd like to avoid an absolute link if I can. Is there any other way?

1

Your first attempt does not work because get_template_directory_uri returns the URL to your theme directory.

Your second attempt will work, but if you don't want to hard code the domain you could either try using a root relative path or fetching the domain portion of your site url.

Relative path:

wp_register_script('circle-bounce', '/js/general.js', array( 'jquery', 'jquery-effects-core', 'jquery-effects-bounce'));

Domain:

$parts = parse_url(site_url());
$domain_url = $parts['scheme'] . '://' . $parts['host'];
wp_register_script('circle-bounce', $domain_url . '/js/general.js', array( 'jquery', 'jquery-effects-core', 'jquery-effects-bounce'));
  • I tried root relative and it ends up thinking that the subdomain is the root. With the relative path, it actually links to www.website.com/blog/js/general.js. It should be going to the actual root: www.website.com/js/general.js. – amylynn83 Jun 7 '15 at 23:57
  • The script that you provided works. What is the dollar sign for? Is it JavaScript or something else? – amylynn83 Jun 8 '15 at 0:02
  • 1
    In PHP all variables are declared using a $. As in $myVar = 'test';. In this case $parts and $domain_url are variables. – Mathew Tinsley Jun 8 '15 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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