0

I'm using the PHP file_get_contents() function to retrieve and echo the contents of an SVG-file.

<?php echo file_get_contents( get_stylesheet_directory_uri() . '/assets/images/Search_Glyph.svg' ); ?>

I checked the theme with the Wordpress.org theme checker and I am currently resolving all the issues. One of the issues is the use of file_get_contents.

It gives me the following warning:

WARNING: file_get_contents was found in the file header.php File operations should use the WP_Filesystem methods instead of direct PHP filesystem calls.

I tried finding information about the $wp_filesystem thing, but there is very little information available and even less examples (to be honest, I'm not totally sure if that's the correct function to use).

How can I use a Wordpress function to retrieve a file and echo the contents of it in a PHP-file?

I'm really at loss what I should do with this. All help is very much appreciated!

9
  • What is the theme checker you're using? – Tom J Nowell Jul 4 '20 at 18:21
  • Just replace file_get_contents($path) with WP_Filesystem_Direct::get_contents($path) – Sabbir Hasan Jul 4 '20 at 18:24
  • @TomJNowell I'm using the Wordpress.org theme checker to identify common mistakes. Ultimately it's for Envato, and this plugin gives me an idea of the quality and the pain points. – ralphjsmit Jul 4 '20 at 18:29
  • Hi @SabbirHasan thanks for the suggestion! I changed it to <?php echo WP_Filesystem_Direct::get_contents(get_stylesheet_directory_uri() . '/assets/images/Search_Glyph.svg'); ?> but when I try to save it, it gives an error (I added it via the theme editor). The error is: (1) – ralphjsmit Jul 4 '20 at 18:33
  • Uncaught Error: Class 'WP_Filesystem_Direct' not found in wp-content/themes/ambition-theme/header.php:133 Stack trace: #0 wp-includes/template.php(723): require_once() #1 wp-includes/template.php(672): load_template('/home/u36816p32...', true) #2 wp-includes/general-template.php(41): locate_template(Array, true) #3 wp-content/themes/ambition-theme/home.php(1): get_header() #4 wp-includes/template-loader.php(106): include('/home/u36816p32...') #5 wp-blog-header.php(19): require_once('/home/u36816p32...') #6 /home/u36816p32xxxx/domains/creativec (2) – ralphjsmit Jul 4 '20 at 18:33
1

Firstly add this two line to your functions.php file. Sorry I forgot to mention about this earlier.

require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';

now you can use WP_Filesystem_Direct::get_contents($path) instead of file_get_contents($path)

1

Solution 01

$wp_filesystem = new WP_Filesystem_Direct(null);
$svg= $wp_filesystem->get_contents(get_stylesheet_directory_uri() . '/assets/images/Search_Glyph.svg');

Solution 02

$remote_svg_file = wp_remote_get(get_stylesheet_directory_uri() . '/assets/images/Search_Glyph.svg');
$svg_content = wp_remote_retrieve_body($remote_svg_file );
3
  • Hi @EjazULHaq thanks for the solution! I already got an answer from SabbirHasan in the comments and I asked him to post it as answer. If he does it, I'll mark that as correct. Otherwise I'll mark your answer as correct next week. I did upvote it. Thanks! Ralph – ralphjsmit Jul 4 '20 at 18:40
  • Thanks @ralphsmit – Ejaz UL Haq Jul 4 '20 at 18:41
  • Note that solution 2 will involve a HTTP request, making it very slow, and on a lot of systems means it'll go out on to the open web then loop back on itself. Also this answer doesn't explain anything, edit it to explain what it does so it isn't just copy pasta otherwise it isn't useful for new developers – Tom J Nowell Jul 4 '20 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.