Theme Development, WordPress

file_get_contents was found in the file – A complete solution to overcome it

If you are developing a premium theme for WordPress.org or Themeforest, then you must be familiar with the following error “file_get_contents was found in the file”. If you encounter this error message, operations should use the WP_Filesystem method instead of the direct PHP filesystem. To remove this warning, you have to use WP_Filesystem instead of file_get_contents to acquire content from a specific file.

I shall guide you on how to overcome the file_get_contents issue from your theme check warning. Let us take a look at the function to identify what caused the aforementioned error.

 

public function icon_generator() {
    $file = THEMEOO_THEME_DIR . 'inc/icon/fa-icons.json';
    if ( is_readable( $file ) ) {
      $icons = file_get_contents( $file ); // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown
      if ( ! empty( $icons ) ) {
        $icons = json_decode( $icons );
        if ( isset( $icons->icons ) && ! empty( $icons->icons ) ) {
          foreach ( $icons->icons as $icon ) {
            echo '<a href="#" data-themeoo-icon="ti-' . esc_attr( $icon ) . '"><span class="ti-' . esc_attr( $icon ) . '"></a>';
          }
        }
      }
    }
    die();
}

The problem arose when I tried to read the file content by using the line shown below:

 

$icons = file_get_contents( $file );

Here, I was using file_get_contents to get the data from the file declared under the variable “$file”. That is an incorrect way to do so in WordPress and, hence, theme check for WordPress.org, and ThemeForest will show you a notice. Moving on, let us see how the same function can be replaced using the correct WordPress way.

 

public function icon_generator() {
  $file = THEMEOO_THEME_DIR . 'inc/icon/fa-icons.json';
  if ( is_readable( $file ) ) {
    $icons = WP_Filesystem_Direct::get_contents( $file ); // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown
    if ( ! empty( $icons ) ) {
      $icons = json_decode( $icons );
      if ( isset( $icons->icons ) && ! empty( $icons->icons ) ) {
        foreach ( $icons->icons as $icon ) {
          echo '<a href="#" data-themeoo-icon="ti-' . esc_attr( $icon ) . '"><span class="ti-' . esc_attr( $icon ) . '"></a>';
        }
      }
    }
  }
  die();
}

Remember that you are using WP_Filesystem_Direct and so, you have to include the following file below to your file where you declared the icon_generator function like above.

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

If you do all the codes in functions.php, then just include those files in the functions.php file.

Wrapping Up

It will not only solve your warning but also work the same as the first function. You can see our other articles to learn

Beginner’s Guide creates a full-width page in WordPress.

How to remove or add capabilities to user roles in your WordPress website.

How to remove WordPress version number from your website.

We hope this article will help you. If you like this article, please like our Facebook Page to stay connected.

You Might Also Like