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 Answer
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'));

Leave a Reply

Your email address will not be published. Required fields are marked *