今日ご紹介するのは、エンコード中に発生した問題についてです。 WP プラグインのオプション フォームは、いくつかのフォーム値変数を定義できます。WP は、$_POST を自分で処理することなく、フォームによって送信されたコンテンツの処理、保存、変更に直接役立ちます。しかし、送信したいフォームのコンテンツが動的に変化する配列の場合はどうなるでしょうか?たとえば、フォームは動的に生成された入力ボックスのリストです。$_POST を使用せずに、このデータを配列変数に直接パッケージ化して WP に渡すにはどうすればよいですか?
PHP フォーム配列を突然思いつきました:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"1"</span><span> </span><span class="tag">/></span><span> </span></span></li> <li> <span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"2"</span><span> </span><span class="tag">/></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"3"</span><span> </span><span class="tag">/></span><span> </span> </li> <li><span>$_POST结果为: </span></li> <li class="alt"><span>Array </span></li> <li><span>( </span></li> <li class="alt"> <span>[a] =</span><span class="tag">></span><span> Array </span> </li> <li><span>( </span></li> <li class="alt"> <span>[0] =</span><span class="tag">></span><span> 1 </span> </li> <li> <span>[1] =</span><span class="tag">></span><span> 2 </span> </li> <li class="alt"> <span>[2] =</span><span class="tag">></span><span> 3 </span> </li> <li><span>) </span></li> <li class="alt"><span>) </span></li> </ol>
次に、フォームに次のような名前を付けます:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[2]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"1"</span><span> </span><span class="tag">/></span><span> </span></span></li> <li> <span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[5]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"2"</span><span> </span><span class="tag">/></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[9]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"3"</span><span> </span><span class="tag">/></span><span> </span> </li> <li><span>$_POST输出结果为: </span></li> <li class="alt"><span>Array </span></li> <li><span>( </span></li> <li class="alt"> <span>[a] =</span><span class="tag">></span><span> Array </span> </li> <li><span>( </span></li> <li class="alt"> <span>[2] =</span><span class="tag">></span><span> 1 </span> </li> <li> <span>[5] =</span><span class="tag">></span><span> 2 </span> </li> <li class="alt"> <span>[9] =</span><span class="tag">></span><span> 3 </span> </li> <li><span>) </span></li> <li class="alt"><span>) </span></li> </ol>
さて、PHP フォーム配列のテストを続けます:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[aa]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"1"</span><span> </span><span class="tag">/></span><span> </span></span></li> <li> <span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[bb]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"2"</span><span> </span><span class="tag">/></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[cc]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"3"</span><span> </span><span class="tag">/></span><span> </span> </li> <li><span> </span></li> <li class="alt"><span>$_POST输出结果为: </span></li> <li><span>Array </span></li> <li class="alt"><span>( </span></li> <li> <span>[a] =</span><span class="tag">></span><span> Array </span> </li> <li class="alt"><span>( </span></li> <li> <span>[aa] =</span><span class="tag">></span><span> 1 </span> </li> <li class="alt"> <span>[bb] =</span><span class="tag">></span><span> 2 </span> </li> <li> <span>[cc] =</span><span class="tag">></span><span> 3 </span> </li> <li class="alt"><span>)</span></li> <li class="alt"><span>)</span></li> </ol>
次に、多次元配列も成功しました。うわー、すごいですね!
動的に生成された PHP フォーム配列は、JavaScript に依存せずに、配列変数の形式で大量のデータを送信できるため、この小さな WordPress プラグインの開発の問題を解決できます。