Wordpress Contact Form 7 メールアドレス確認を有効にする方法

WordpressのContact Form 7 には以下のようにメールアドレスを2回入力させて確認を有効にする機能がありますが

<div class="code-frame" data-lang="html"><div class="highlight"><pre>
[email* your_email]
[email* your_email_confirm]

</pre></div></div>

こういう記述をしますが、プラグインを入れただけでは機能しないみたいです。


<p>Wordpress テーマの編集で、functions.php に以下のコードを記述する必要があります。</p>

<div class="code-frame" data-lang="php"><div class="highlight"><pre>
<span class="k">function</span> <span class="nf">wpcf7_main_validation_filter</span><span class="p">(</span> <span class="nv">$result</span><span class="p">,</span> <span class="nv">$tag</span> <span class="p">)</span> <span class="p">{</span>
  <span class="nv">$type</span> <span class="o">=</span> <span class="nv">$tag</span><span class="p">[</span><span class="s1">'type'</span><span class="p">];</span>
  <span class="nv">$name</span> <span class="o">=</span> <span class="nv">$tag</span><span class="p">[</span><span class="s1">'name'</span><span class="p">];</span>
  <span class="nv">$_POST</span><span class="p">[</span><span class="nv">$name</span><span class="p">]</span> <span class="o">=</span> <span class="nb">trim</span><span class="p">(</span> <span class="nb">strtr</span><span class="p">(</span> <span class="p">(</span><span class="nx">string</span><span class="p">)</span> <span class="nv">$_POST</span><span class="p">[</span><span class="nv">$name</span><span class="p">],</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">,</span> <span class="s2">" "</span> <span class="p">)</span> <span class="p">);</span>
  <span class="k">if</span> <span class="p">(</span> <span class="s1">'email'</span> <span class="o">==</span> <span class="nv">$type</span> <span class="o">||</span> <span class="s1">'email*'</span> <span class="o">==</span> <span class="nv">$type</span> <span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nb">preg_match</span><span class="p">(</span><span class="s1">'/(.*)_confirm$/'</span><span class="p">,</span> <span class="nv">$name</span><span class="p">,</span> <span class="nv">$matches</span><span class="p">)){</span>
      <span class="nv">$target_name</span> <span class="o">=</span> <span class="nv">$matches</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
      <span class="k">if</span> <span class="p">(</span><span class="nv">$_POST</span><span class="p">[</span><span class="nv">$name</span><span class="p">]</span> <span class="o">!=</span> <span class="nv">$_POST</span><span class="p">[</span><span class="nv">$target_name</span><span class="p">])</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">method_exists</span><span class="p">(</span><span class="nv">$result</span><span class="p">,</span> <span class="s1">'invalidate'</span><span class="p">))</span> <span class="p">{</span>
          <span class="nv">$result</span><span class="o">-&gt;</span><span class="na">invalidate</span><span class="p">(</span> <span class="nv">$tag</span><span class="p">,</span><span class="s2">"確認用のメールアドレスが一致していません"</span><span class="p">);</span>
      <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
          <span class="nv">$result</span><span class="p">[</span><span class="s1">'valid'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
          <span class="nv">$result</span><span class="p">[</span><span class="s1">'reason'</span><span class="p">][</span><span class="nv">$name</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'確認用のメールアドレスが一致していません'</span><span class="p">;</span>
        <span class="p">}</span>
      <span class="p">}</span>
    <span class="p">}</span>
  <span class="p">}</span>
  <span class="k">return</span> <span class="nv">$result</span><span class="p">;</span>
<span class="p">}</span>

<span class="nx">add_filter</span><span class="p">(</span> <span class="s1">'wpcf7_validate_email'</span><span class="p">,</span> <span class="s1">'wpcf7_main_validation_filter'</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">2</span> <span class="p">);</span>
<span class="nx">add_filter</span><span class="p">(</span> <span class="s1">'wpcf7_validate_email*'</span><span class="p">,</span> <span class="s1">'wpcf7_main_validation_filter'</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">2</span> <span class="p">);</span>

</pre></div></div>