Is file_get_contents() the only way for plugins reading local files OR does WP_Filesystem_Direct::get_contents() even work?

Background

I’m developing a plugin for WordPress. I initially used file_get_contents() for two purposes:

  1. A remote file (which I changed to wp_remote_post())
  2. A local file in the plugin dir (what this question is about)

WordPress staff told me to NOT use file_get_contents() for remote _POST requests (usage 1. above), but to use wp_remost_post() instead.

From their email concerning usage 1. (above):

Using file_get_contents on remote files

Many hosts block the use of file_get_contents on remote content. This is a security measure that we fully endorse.

Thankfully, WordPress comes with an extensive HTTP API that can be used instead. It’s fast and more extensive than most home-grown alternatives. It’ll fall back to curl if it has to, but it’ll use a lot of WordPress’ native functionality first.

So, I did. But…

Solving usage 1. A remote file (above) got me to reading and thinking about a better tool for usage 2. A local file (above)

In the WordPress HTTP API docs, there is no alternative for file_get_contents() on local files, nothing like wp_file_get_contents().

(I understand this may seem strange since local files of a plugin are often static, but I am writing extra files to save cost, and I politely don’t want to open that discussion. Besides, this approved plugin from someone else, code here, uses file_get_contents(), thus proving my own need is legitimate, but it doesn’t prove that file_get_contents() is ‘proper’ WordPress-practice.)

From this blog post, clearly this was a big discussion for file_get_contents() for remote files. But, I don’t see much on the web about a WordPress function for local files. From the post:

Like many others I’ve used the native PHP function file_get_contents() to receive the content from a remote file because the functions is very easy to use. But is it the best way to do that? I see many scripts using that function and even WordPress plugins are using this function while WordPress has a native function to receive remote content.

…I want to take that discussion to local files and build this plugin right.

My code (current and attempted)

I simply need to confirm whether a local file in the plugin directory exists, then confirm whether it contains a certain string. I thought it best to use file_get_contents() for local files in PHP from this question on SE. But, this is WordPress. Currently, I am using:

if ( ( ! $wp_filesystem->exists ( $check_this_file ) )
 || (strpos ( file_get_contents ( $check_this_file ),
    $check_this_string ) === false ) )

I tried using WP_Filesystem_Direct::get_contents instead:

if ( ( ! $wp_filesystem->exists ( $check_this_file ) )
 || (strpos ( WP_Filesystem_Direct::get_contents ( $check_this_file ),
    $check_this_string ) === false ) )

…but I got this error:

Deprecated: Non-static method WP_Filesystem_Direct::get_contents() should not be called statically

What should I do?

I’m not sure because there isn’t even a #get-contents tag on WP.SE at the time of asking, but the function exists both in the framework and in the docs.

I want to know the "WordPress way" for plugins to read a plugin file’s contents for a string test:

  • file_get_contents() (what I have now)
  • WP_Filesystem_Direct::get_contents() (but how do I make it work without the error?)
  • or some proper-reliable usage of wp_filesystem() or similar

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Is file_get_contents() the only way for plugins reading local files OR does WP_Filesystem_Direct::get_contents() even work?

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.