admin管理员组文章数量:1122846
I need to use file_get_contents()
in my theme to include an SVG file. As far as I know, an obvious way to do this would be the following:
file_get_contents(".svg");
However, if I do that, my server has to call itself and chokes up if there’s many visitors at the same time. When I edit the code in the following way, it gets much better, as the server fetches the file locally, without calling itself from outside:
file_get_contents("wp-content/themes/theme_name/file.svg");
There is a function that returns the path to the theme directory, get_template_directory_uri()
. But it also returns the hostname — — which I don’t need. I guess I could use WP constants listed in the WP Codex, but it specifically says “these [the constants] should not be used directly by plugins or themes”.
Is there a similar function that doesn’t include the hostname in what it returns?
I need to use file_get_contents()
in my theme to include an SVG file. As far as I know, an obvious way to do this would be the following:
file_get_contents("https://example.com/wp-content/themes/theme_name/file.svg");
However, if I do that, my server has to call itself and chokes up if there’s many visitors at the same time. When I edit the code in the following way, it gets much better, as the server fetches the file locally, without calling itself from outside:
file_get_contents("wp-content/themes/theme_name/file.svg");
There is a function that returns the path to the theme directory, get_template_directory_uri()
. But it also returns the hostname — https://example.com
— which I don’t need. I guess I could use WP constants listed in the WP Codex, but it specifically says “these [the constants] should not be used directly by plugins or themes”.
Is there a similar function that doesn’t include the hostname in what it returns?
Share Improve this question asked Jul 8, 2024 at 15:13 Ivan VetoshkinIvan Vetoshkin 111 bronze badge 3 |1 Answer
Reset to default 2Yes get_template_directory
:
get_template_directory(): string
Retrieves template directory path for the active theme. Returns an absolute server path (eg: /home/user/public_html/wp-content/themes/my_theme), not a URI.
In the case a child theme is being used, the absolute path to the parent theme directory will be returned. Use get_stylesheet_directory() to get the absolute path to the child theme directory.
To retrieve the URI of the stylesheet directory use get_stylesheet_directory_uri() instead.
https://developer.wordpress.org/reference/functions/get_template_directory/
There's also get_stylesheet_directory
which refers to the current theme, whereas get_template_directory
always refers to the parent theme ( if there is no child theme then they will return the same value )
$svg = file_get_contents( get_template_directory() . '/file.svg' );
本文标签: phpIs there a way to get a path to the theme directory without the server name
版权声明:本文标题:php - Is there a way to get a path to the theme directory without the server name? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301464a1931185.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
file_get_contents
used on a URL won't work on many servers due to security protections, and the WP Codex was deprecated in favor of the developer hub and code reference several years ago at developer.wordpress.org – Tom J Nowell ♦ Commented Jul 8, 2024 at 15:34