2

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
    Commented Jul 4, 2020 at 18:21
  • Just replace file_get_contents($path) with WP_Filesystem_Direct::get_contents($path) Commented Jul 4, 2020 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
    Commented Jul 4, 2020 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
    Commented Jul 4, 2020 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
    Commented Jul 4, 2020 at 18:33

2 Answers 2

4

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)

Update: 20 July 2023

After some digging it's safe to say that you should use the file system like this:

$filesystem = new WP_Filesystem_Direct( true );

// Now use it like this

$filesystem->get_contents($path)

All available methods can be found here https://developer.wordpress.org/reference/files/wp-admin/includes/class-wp-filesystem-direct.php/

1
  • Non-static method WP_Filesystem_Direct::get_contents() cannot be called statically
    – Azamat
    Commented Jul 18, 2023 at 19:49
4

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 );
5
  • 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
    Commented Jul 4, 2020 at 18:40
  • Thanks @ralphsmit Commented Jul 4, 2020 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
    Commented Jul 4, 2020 at 18:49
  • Solution 1 will not work when the project uses Composer with PSR-4 autoloading, as WP_Filesystem_Direct does not conform to PSR-4 requirements. Commented May 11, 2022 at 17:54
  • @MarcioDuarte, I have updated so now it will work with PSR-4 and it is a standard solution that you should add \ before the class name when you are calling any call without namespace in the PSR-4. Commented Mar 18, 2023 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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