Background
I am creating a plugin for WordPress. I initially used file_get_contents()
for 2 functions:
- A distant file (which I modified to
wp_remote_post()
) - A neighborhood file within the plugin dir (what this query is about)
WordPress workers instructed me to NOT use file_get_contents()
for distant _POST
requests (utilization 1. above), however to make use of wp_remost_post()
as a substitute.
From their e mail regarding utilization 1. (above):
Utilizing file_get_contents on distant recordsdata
Many hosts block the usage of file_get_contents on distant content material. This can be a safety measure that we absolutely endorse.
Fortunately, WordPress comes with an intensive HTTP API that can be utilized as a substitute. It’s quick and extra intensive than most home-grown alternate options. It’ll fall again to curve if it has to, nevertheless it’ll use lots of WordPress’ native performance first.
So, I did. However…
Fixing utilization 1. A distant file (above) bought me to studying and fascinated about a greater instrument for utilization 2. A neighborhood file (above)
Within the WordPress HTTP API docs, there isn’t a different for file_get_contents()
on native recordsdata, nothing like wp_file_get_contents()
.
(I perceive this will likely appear unusual since native recordsdata of a plugin are sometimes static, however I’m writing additional recordsdata to save lots of value, and I politely do not need to open that dialogue. Moreover, this accepted plugin from another person, code right here, makes use of file_get_contents()
, thus proving my very own want is authentic, nevertheless it does not show that file_get_contents()
is ‘correct’ WordPress-practice.)
From this weblog publish, clearly this was a giant dialogue for file_get_contents()
for distant recordsdata. However, I do not see a lot on the internet a couple of WordPress operate for native recordsdata. From the publish:
Like many others I’ve used the native PHP operate file_get_contents() to obtain the content material from a distant file as a result of the capabilities may be very simple to make use of. However is it one of the simplest ways to do this? I see many scripts utilizing that operate and even WordPress plugins are utilizing this operate whereas WordPress has a local operate to obtain distant content material.
…I need to take that dialogue to native recordsdata and construct this plugin proper.
My code (present and tried)
I merely want to verify whether or not a neighborhood file within the plugin listing exists, then verify whether or not it accommodates a sure string. I believed it greatest to make use of file_get_contents()
for native recordsdata in PHP from this query on SE. However, that is WordPress. At the moment, I’m utilizing:
if ( ( ! $wp_filesystem->exists ( $check_this_file ) )
|| (strpos ( file_get_contents ( $check_this_file ),
$check_this_string ) === false ) )
I attempted utilizing WP_Filesystem_Direct::get_contents as a substitute:
if ( ( ! $wp_filesystem->exists ( $check_this_file ) )
|| (strpos ( WP_Filesystem_Direct::get_contents ( $check_this_file ),
$check_this_string ) === false ) )
…however I bought this error:
Deprecated: Non-static technique WP_Filesystem_Direct::get_contents() shouldn't be referred to as statically
What ought to I do?
I am unsure as a result of there is not even a #get-contents tag on WP.SE on the time of asking, however the operate exists each within the framework and within the docs.
I need to know the "WordPress way" for plugins to learn a plugin file’s contents for a string take a look at:
file_get_contents()
(what I’ve now)WP_Filesystem_Direct::get_contents()
(however how do I make it work with out the error?)- or some proper-reliable utilization of
wp_filesystem()
or comparable