将fopen / fwrite转换为WordPress WP_Filesystem

问题描述 投票:0回答:1

我正在主题中使用Easy Theme Child插件中的代码,但它失败了Theme Check插件,并出现以下错误:

WARNING: fopen/fwrite was found in the file grtsth-easy-child-theme-resources.php and resources.php File operations should use the `WP_Filesystem` methods instead of direct PHP filesystem calls.

所以我尝试转换问题代码:

$create_style_css    = fopen( $file_style_css, 'w+' );
$create_function_php = fopen( $file_function_php, 'w+' );
fwrite( $create_style_css, $stylesheet_data );
fwrite( $create_function_php, $function_php_data );

至:

WP_Filesystem();
global $wp_filesystem;

$create_style_css    = $wp_filesystem->get_contents($file_style_css);
$create_function_php = $wp_filesystem->get_contents($file_function_php);
$wp_filesystem->put_contents($create_style_css, $stylesheet_data);
$wp_filesystem->put_contents($create_function_php, $function_php_data);

但是,它不起作用。没有错误。只是不创建子主题文件。我对WP_Filesystem不太熟悉,所以有什么想法我做错了吗?

谢谢

php wordpress wordpress-theming fopen fwrite
1个回答
0
投票

((这更多是评论而不是答案-无法评论!)

这是警告,对于许多用户,您可以忽略。您的主题将继续正常运行。

[警告​​只是说主题是作为Web服务器用户读取/写入文件,而WP Filesystem可以利用其他文件操作方法,这在某些情况下是有益的。

我不会费心尝试将读写操作更改为WP Filesystem,特别是如果您不打算将代码部署到广泛的环境中(我假设您只是在谈论一个网站)。>>

但是,如果您真的想继续...

进行文件操作之前,您需要set / get access Credentials

请参阅此方便的分步指南:https://www.sitepoint.com/introduction-to-the-wordpress-filesystem-api/

© www.soinside.com 2019 - 2024. All rights reserved.