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
  • note that 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
  • try this : developer.wordpress.org/reference/functions/… – mmm Commented Jul 8, 2024 at 15:35
  • @mmm, sorry, but that’s not it — see my comment to Tom J Nowell’s answer. – Ivan Vetoshkin Commented Jul 8, 2024 at 16:29
Add a comment  | 

1 Answer 1

Reset to default 2

Yes 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