<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WP_Filesystem &#8211; WordPress Memo</title>
	<atom:link href="https://blog.riverforest-wp.info/tag/wp_filesystem/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.riverforest-wp.info</link>
	<description>WordPress に関する覚え書き</description>
	<lastBuildDate>Fri, 09 May 2025 12:48:52 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.riverforest-wp.info/wp-content/uploads/2018/05/cropped-icon-256x256-1-32x32.png</url>
	<title>WP_Filesystem &#8211; WordPress Memo</title>
	<link>https://blog.riverforest-wp.info</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>WordPress での CSV 出力について</title>
		<link>https://blog.riverforest-wp.info/wp-csv-export-function/</link>
		
		<dc:creator><![CDATA[Katsushi Kawamori]]></dc:creator>
		<pubDate>Wed, 20 Dec 2023 12:44:23 +0000</pubDate>
				<category><![CDATA[Function]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[SplFileObject]]></category>
		<category><![CDATA[WP_Filesystem]]></category>
		<guid isPermaLink="false">https://blog.riverforest-wp.info/?p=3619</guid>

					<description><![CDATA[はじめに 最近、Plugin Check というプラグインチームが作成したプラグインで自分のプラグインをチェックしているのですが、以下の様な警告が出てきます。 「json_encode を、wp_json_encode  [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h3 class="wp-block-heading">はじめに</h3>



<p>最近、<strong>Plugin Check</strong> というプラグインチームが作成したプラグインで自分のプラグインをチェックしているのですが、以下の様な警告が出てきます。</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;69cc634952872&quot;}" data-wp-interactive="core/image" data-wp-key="69cc634952872" class="wp-block-image aligncenter size-full wp-lightbox-container"><img fetchpriority="high" loading="eager" decoding="async" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on-window--resize="callbacks.setButtonStyles" width="860" height="758" src="https://blog.riverforest-wp.info/wp-content/uploads/20231220/plugincheck_bad.png" alt="" class="wp-image-3629" srcset="https://blog.riverforest-wp.info/wp-content/uploads/20231220/plugincheck_bad.png 860w, https://blog.riverforest-wp.info/wp-content/uploads/20231220/plugincheck_bad-300x264.png 300w, https://blog.riverforest-wp.info/wp-content/uploads/20231220/plugincheck_bad-768x677.png 768w" sizes="(max-width: 860px) 100vw, 860px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			aria-label="拡大する"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.imageButtonRight"
			data-wp-style--top="state.imageButtonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button></figure>



<p>「<a href="https://www.php.net/manual/ja/function.json-encode.php">json_encode</a> を、<a href="https://developer.wordpress.org/reference/functions/wp_json_encode/">wp_json_encode</a> にしなさい」というのと、「<a href="https://www.php.net/manual/ja/function.fopen.php">fopen</a>、<a href="https://www.php.net/manual/ja/function.fclose.php">fclose</a>、<a href="https://www.php.net/manual/ja/function.fputcsv.php">fputcsv</a> などの php のファイルシステムは使用せずに、<a href="https://developer.wordpress.org/reference/functions/wp_filesystem/">WP_Filesystem</a> を使用しなさい」というのが主です。</p>



<div class="pluginstatsview-card-wrap">
	<div>
		<img decoding="async" src="https://ps.w.org/plugin-check/assets/icon.svg?rev=3166100" alt="Plugin Check (PCP) のアイコン" class="pluginstatsview-card-icon" />
		<div class="pluginstatsview-after-icon">
			<div class="pluginstatsview-bold"><a href="https://github.com/WordPress/plugin-check" class="pluginstatsview-astyle">Plugin Check (PCP)</a></div>
			<div class="pluginstatsview-small">Plugin Check は WordPress.org のツールで、プラグインがディレクトリの要件を満たし、様々なベストプラク&#8230;</div>
			<div class="pluginstatsview-small">作者: <a href="https://profiles.wordpress.org/wordpressdotorg/">WordPress.org</a></div>
			<div style="clear: both;"></div>
		</div>
	</div>
	<div style="clear: both;"></div>

	<div class="pluginstatsview-small">
		<span class="pluginstatsview-card-left"><a href="https://github.com/WordPress/plugin-check#reviews" title="4.4(5点満点中)" class="pluginstatsview-astyle">	<span class="pluginstatsview-stars">
					<span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-half"></span>
			</span>
	</a>(32)</span>
		<span class="pluginstatsview-card-right">最終更新: 2週間 前</span>
	</div>
	<div class="pluginstatsview-small">
		<span class="pluginstatsview-card-left">8,000以上サイトで有効化中</span>
		<span class="pluginstatsview-card-right">検証済み: 7.0</span>
	</div>
	<div class="pluginstatsview-small">
		<span class="pluginstatsview-card-left">ダウンロード: <a href="https://downloads.wordpress.org/plugin/plugin-check.1.9.0.zip" class="dashicons dashicons-download pluginstatsview-download"></a></span>
		<span class="pluginstatsview-card-right">リリース: 2011-09-04</span>
	</div>
	<div class="pluginstatsview-small pluginstatsview-card-center">
		2026-03-31 09:30 時点の統計	</div>
</div>



<p>WP_Filesystem に関しては、テーマなどで、<a href="https://www.php.net/manual/ja/function.file-get-contents.php">file_get_contents</a>、<a href="https://www.php.net/manual/ja/function.file-put-contents.php">file_put_contents</a> などの代わりに使用するように推奨していたようですが、プラグインに関しても推奨するようになってきたのかという感じです。しかし、必ずしもすべてこの通りにしなければならないというものでもないようで、その辺りの事が、<a href="https://ja.wordpress.org/plugins/plugin-check/#why%20does%20it%20flag%20something%20as%20bad%3F">FAQ</a> に書かれています。実際、上記の非推奨のものを使用していてトラブルになったおぼえはありません。</p>



<p>自分の場合には、CSV 出力関連ですが、WP_Filesystem には、fputcsv の代替えのものがありません。file_put_contents の代替えのものはあるのですがコードが煩雑になり遠回りになってしまいます。そこで、<a href="https://www.php.net/manual/ja/class.splfileobject.php">SplFileObject クラス</a> を使用してみたところ、<strong>Plugin Check</strong> に怒られなく？なりました。</p>



<h3 class="wp-block-heading">コード</h3>



<p>$csvs は、CSV にするための配列とします。$csv_file は、フルパスを含むファイル名です。フィルターに関しては無くても動作しますが、Windows の Excel で直接開きたい場合には、必要です。</p>



<p>fputcsv を用いたコード</p>



<pre class="prism line-numbers"><code class="language-php language-html">$f = fopen( $csv_file, 'w' );
stream_filter_prepend( $f, 'convert.iconv.utf-8/cp932//TRANSLIT' );
if ( $f ) {
	foreach ( $csvs as $line ) {
		fputcsv( $f, $line );
	}
}
fclose( $f );</code></pre>



<p>SplFileObject クラスを用いたコード</p>



<pre class="prism line-numbers"><code class="language-php language-html">$filter = urlencode( 'convert.iconv.utf-8/cp932//TRANSLIT' );
$file = new SplFileObject( 'php://filter/write=' . $filter . '/resource=' . $csv_file, 'a' );
foreach ( $csvs as $line ) {
	$file->fputcsv( $line );
}
$file = null;</code></pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
