Wordpress in subdirectory using wrong path for enqueing scripts

590 Views Asked by At

I have been searching for a while but I haven't really seen anyone talk about this or found solutions. At least nothing other then some hacks to help out from the functions.php

For a WordPress install at work I had to move the install into its own folder. I used the guide here: https://wordpress.org/support/article/giving-wordpress-its-own-directory/#method-ii-with-url-change

Everything is working great and the folder structure is as expected:

- /
|-- Wordpress
|-- wp-content
  |-- themes
    |-- WebsiteTheme

So no worries there. The functions.php has, for the moment, the following 2 lines in the wp-config.php

define( 'WP_HOME', 'https://dev.website.nl/' );
define( 'WP_SITEURL', 'https://dev.website.nl/wordpress' );

All in accordance with the guide from the official WordPress website. But here is where things go weird. When I try to Enqueue a script like this

$path = '/wp-content/themes/website/js/app.js';
wp_register_script('someName-'.$entry, $path, [], false, true);        
wp_enqueue_script('someName-'.$entry);

the path eventually is set as

/wordpress/wp-content/themes/website/js/app.js

which of course won't work. It looks like the path is pre-fixed with the SITEURL instead of the HOME url.

Currently I am using a filter that hooks into the script_loader_tag and str_replace the "/wordpress" part out of the path if a conditional statement is true based on the name I gave the script. But as I just followed the WordPress guide set this up, I thought this would not be an issue. Any idea if I am missing something here?

Thanks

1

There are 1 best solutions below

0
On

The second parameter of wp_register_script() accepts $src as the full URL of the script OR the path of the script relative to the WordPress root directory.

To prevent this issue caused by the latter default, you'd probably be better off passing the full URL. I think you can do so by prepending get_template_directory_uri() or get_stylesheet_directory_uri().

E.g.:

$path = '/js/app.js';
wp_register_script('someName-'.$entry, get_template_directory_uri() . $path, [], false, true);