見出し画像

[Wordpress]フォームからPOSTすると404になってハマった話

新規でフォームを作成する際、これまでと同じ設定なのになぜか入力画面から確認画面に遷移する時、404判定になって相当ハマったというお話。


1. コード概要

Wordpressのglobal関数である$postを利用して、独自のMVCシステムに飛ばす作りになっているのだが、その判定の際以下のような判定になる。
(実際はkeyやvalueは異なる)

- POST送信した際

array(
  'language' => 'ja',
  'judge' => 'error',
  'method' => null
);

で、何でこれがおかしいかというと、確認画面ページに直URLでアクセスした場合(通常は直はダメなのでリダイレクトは入れるが、デバッグのため解放)はこんな表示にならない。

- 直でアクセスした場合

array(
  'language' => 'ja',
  'judge' => 'form',
  'method' => 'confirm'
);

ちゃんと取れている…。
他の自作フォームシステムを踏襲しているので、こんなことにはならないはずだと、かなり長い時間ハマってしまった。

2. 先に結論

フォームのname属性にWordpressの予約語を使ってしまっていたことによるエラー。
今回私がハマったのは、このフォームだけ名前の入力欄が姓名わかれていなかったことで、そのname属性の指定を誤ったこと。

- 従来までのコード

細かい部分は簡略化してHTMLコード例を示しておく。

<form>
  <div>
    <label for="lname">姓:</label>
    <input type="text" id="lname" name="lname">
    <label for="fname">名:</label>
    <input type="text" id="fname" name="fname">
  </div>
</form>

こんな感じで姓名わかれてる時は何も気にしなくていいのだが、問題は分かれてないときのname属性。
脳死してるとすぐやっちゃう。

- だめなこーど

<form>
  <div>
    <label for="name">名前:</label>
    <input type="text" id="name" name="name">
  </div>
</form>

これ、動かないんだよねー。
POSTすると404エラーになっちゃう。
理由はnameがWordpressの予約語だから。
正しく動かすためには下記のようにしないといけない。

- OKなコード

<form>
  <div>
    <label for="fullname">名前:</label>
    <input type="text" id="fullname" name="fullname">
  </div>
</form>

fullnameとか、引っかからないようにすればちゃんとPOSTしても通ってくれる。
予約語は使わない、というのは非常に基本的なことだけれども、何も考えずに脳死でコーディングしてるとエラーになり、こういった思わぬところにエラーが潜むことがあるので、戒めをこめて。

3. Wordpressの予約語一覧

この予約語は、当然フォームだけではなくて、スラッグなどに使用してもダメ。
なので、基本的にはあらゆるところで予約語は使わないようにしよう。
予約語は以下が一覧。

$$
\begin{array}{|l|l|l|} \hline
\text{attachment} & \text{attachment\_id} & \text{author}\\ \hline
\text{author\_name} & \text{calendar} & \text{cat}\\ \hline
\text{category} & \text{category\_\_and} & \text{category\_\_in}\\ \hline
\text{category\_\_not\_in} & \text{category\_name} & \text{comments\_per\_page}\\ \hline
\text{comments\_popup} & \text{customize\_messenger\_channel} & \text{customized}\\ \hline
\text{cpage} & \text{day} & \text{debug}\\ \hline
\text{error} & \text{exact} & \text{feed}\\ \hline
\text{fields} & \text{hour} & \text{link\_category}\\ \hline
\text{m} & \text{minute} & \text{monthnum}\\ \hline
\text{more} & \text{name} & \text{nav\_menu}\\ \hline
\text{nonce} & \text{nopaging} & \text{offset}\\ \hline
\text{order} & \text{orderby} & \text{p}\\ \hline
\text{page} & \text{page\_id} & \text{paged}\\ \hline
\text{pagename} & \text{pb} & \text{perm}\\ \hline
\text{post} & \text{post\_\_in} & \text{post\_\_not\_in}\\ \hline
\text{post\_format} & \text{post\_mime\_type} & \text{post\_status}\\ \hline
\text{post\_tag} & \text{post\_type} & \text{posts}\\ \hline
\text{posts\_per\_archive\_page} & \text{posts\_per\_page} & \text{preview}\\ \hline
\text{robots} & \text{s} & \text{search}\\ \hline
\text{second} & \text{sentence} & \text{showposts}\\ \hline
\text{static} & \text{subpost} & \text{subpost\_id}\\ \hline
\text{tag} & \text{tag\_\_and} & \text{tag\_\_in}\\ \hline
\text{tag\_\_not\_in} & \text{tag\_id} & \text{tag\_slug\_\_and}\\ \hline
\text{tag\_slug\_\_in} & \text{taxonomy} & \text{tb}\\ \hline
\text{term} & \text{theme} & \text{type}\\ \hline
\text{w} & \text{withcomments} & \text{withoutcomments}\\ \hline
\text{year} & & \\ \hline
\end{array}
$$

4. 参考にさせてもらった記事

https://analyzegear.co.jp/blog/1372
https://cotodama.co/reserved-word/


この記事が気に入ったらサポートをしてみませんか?