0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Wordpress Contact Form 7 でメール確認とラジオ縦並び

Last updated at Posted at 2017-02-13

Wordpressのコンタクトフォームで、メールアドレスを確認の意味で2回入力させてチェックするというよくある入力チェック対応とラジオボタン、チェックボタンを横並びではなく、縦並びにするカスタマイズを行った。 ラジオボタンだけ縦並びにしたかったそれはできなかった。

メール確認用のタグ名は_confirmをつける仕様になっています。

テーマ編集で functions.php 以下のものを追加した。

functions.php
・・・
<?php
add_filter( 'wpcf7_validate_email', 'wpcf7_text_validation_filter_extend', 11, 2 );
add_filter( 'wpcf7_validate_email*', 'wpcf7_text_validation_filter_extend', 11, 2 );
function wpcf7_text_validation_filter_extend( $result, $tag ) {
    $type = $tag['type'];
    $name = $tag['name'];
    $_POST[$name] = trim( strtr( (string) $_POST[$name], "\n", " " ) );
    if ( 'email' == $type || 'email*' == $type ) {
        if (preg_match('/(.*)_confirm$/', $name, $matches)){
            $target_name = $matches[1];
            if ($_POST[$name] != $_POST[$target_name]) {
                if (method_exists($result, 'invalidate')) {
                    $result->invalidate( $tag,"確認用のメールアドレスが一致していません");
                } else {
                    $result['valid'] = false;
                    $result['reason'] = array( $name => '確認用のメールアドレスが一致していません' );
                }
            }
        }
    }
    return $result;
}

?>
<?php
add_filter( 'wpcf7_validate_radio', 'my_validate_radio', 10, 2 );
function my_validate_radio( $result, $tag ) {
    $type = $tag['type'];
    $name = $tag['name'];
 
    if ( 'radio' == $type ) {
        if ( !isset( $_POST[$name] ) || '' == $_POST[$name] ) {
            $result['valid'] = false;
            $result['reason'] = array( $name => wpcf7_get_message( 'invalid_required' ) );
        }
    }
    return $result;
}
?>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?