css - Getting out side of wp root folder from function.php using absolute path

admin2025-06-04  2

Hi Everyone I am having a relative path issue with linking a css file using a absolute path.

Basically I am having the exact same problem as the person here however I do not want to use the URL of the site to link the css file.

Anything I do, word press tries to put its own file path in front of it.

so if i have done this:

wp_enqueue_style('media', $_SERVER["DOCUMENT_ROOT"] . '/assets/css/media.css');

word press will give me this :

<link rel="stylesheet" id="media_style-css" href="http://192.168.1.111/blog/var/www/html/assets/css/media.css?ver=4.0.5" type="text/css" media="all">

and funny enough if I do this:

wp_enqueue_style('main_style', $_SERVER["HTTP_HOST"] . '/assets/css/style.less');

Wordpress decides to give me this....

<link rel="stylesheet" id="main_style-css" href="http://192.168.1.111/blog192.168.1.111/assets/css/style.less?ver=4.0.5" type="text/css" media="all">

Is there any way to actually get to the root of my site via function.php or does wordpress just not let you via absolute path?

Hi Everyone I am having a relative path issue with linking a css file using a absolute path.

Basically I am having the exact same problem as the person here however I do not want to use the URL of the site to link the css file.

Anything I do, word press tries to put its own file path in front of it.

so if i have done this:

wp_enqueue_style('media', $_SERVER["DOCUMENT_ROOT"] . '/assets/css/media.css');

word press will give me this :

<link rel="stylesheet" id="media_style-css" href="http://192.168.1.111/blog/var/www/html/assets/css/media.css?ver=4.0.5" type="text/css" media="all">

and funny enough if I do this:

wp_enqueue_style('main_style', $_SERVER["HTTP_HOST"] . '/assets/css/style.less');

Wordpress decides to give me this....

<link rel="stylesheet" id="main_style-css" href="http://192.168.1.111/blog192.168.1.111/assets/css/style.less?ver=4.0.5" type="text/css" media="all">

Is there any way to actually get to the root of my site via function.php or does wordpress just not let you via absolute path?

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Jun 26, 2015 at 23:13 FloFlo 1114 bronze badges 5
  • 1 What if you just do wp_enqueue_style('main_style', '/assets/css/style.less'); ? – czerspalace Commented Jun 27, 2015 at 0:17
  • href = url only you cannot link files using the path, only the url, when you hear of relative paths being used, they actually mean relative to the url unless used within php, if you want to load files using the path use include / require but you are missing a opportunity to cache the files. Is there any reason you are looking to use a path ? Btw $_SERVER['host'] is client side – David Commented Jun 27, 2015 at 1:41
  • @czerspalace that takes me to the root of the blog because wp puts its root in front. would end up blog/assets/css/... – Flo Commented Jun 27, 2015 at 22:35
  • is Wordpress hosted in a subdirectory of your main site? What I mean is do you have a site at / and WordPress is installed at /blog/ ? – czerspalace Commented Jun 28, 2015 at 10:58
  • @czerspalace yes the root is outside the wordpress directory. – Flo Commented Jun 30, 2015 at 23:41
Add a comment  | 

1 Answer 1

Reset to default 1

If you want the absolute path to your theme's stylesheet directory you could use get_stylesheet_directory(). If you want the URI of your theme's stylesheet directory you could use get_stylesheet_directory_uri().

get_theme_root() will return the absolute path to the themes directory.

get_home_path() will return the absolute filesystem path to the root of the WordPress installation.

Once you've got your absolute path with one of these methods you can then do what you need to do.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749001099a315513.html

最新回复(0)