Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress get SVG file contents
<?php echo file_get_contents( get_stylesheet_directory_uri() . '/img/icons/your-logo-file.svg' ); ?>
@npetryk
Copy link

npetryk commented Dec 3, 2019

Wont using the .._uri() variant make file_get_contents fetch the file over the network, rather than from the filesystem? It seems like using get_stylesheet_directory() with file_get_contents is a bit better

@deavy
Copy link

deavy commented Feb 11, 2020

Since Wordpres 4.7.0 there is cleaner way for getting files from parent/child theme using these function:

@jessedmatlock
Copy link

jessedmatlock commented Dec 3, 2021

Also, try using get_template_directory() instead of get_template_directory_uri()

Use something like:
<?php echo file_get_contents(get_template_directory().'/theme/img/chevron-right-solid.svg'); ?>

Sometimes using get_template_directory_uri() will throw an SSL error

Then, you can target the SVG and style how you wish.
For example:

<button class="btn btn-primary">
  <?php echo file_get_contents(get_template_directory().'/theme/img/chevron-right-solid.svg'); ?>
</button>

CSS might be:

.btn svg { line-height: 1.2; max-height: 1rem;}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment