【问题标题】:Converting fopen/fwrite to WordPress WP_Filesystem将 fopen/fwrite 转换为 WordPress WP_Filesystem
【发布时间】:2020-01-03 14:59:21
【问题描述】:

我在我的主题中使用来自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】:

    (这更像是一个评论而不是一个答案 - 还不能评论!)

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

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

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

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

    您需要set / get access Credentials,然后才能进行文件操作。

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

    【讨论】:

      【解决方案2】:

      您需要首先包含 wp_filesystem 函数并请求凭据(如果之前未使用 wp_filesystem 对象)。详情请查看以下代码:

      global $wp_filesystem;
      if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
          include_once(ABSPATH . 'wp-admin/includes/file.php');
          $creds = request_filesystem_credentials( site_url() );
          wp_filesystem($creds);
      }
      $create_style_css = $wp_filesystem->get_contents($url);
      

      【讨论】:

        猜你喜欢
        • 2015-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-13
        相关资源
        最近更新 更多