京都大学東南アジア地域研究研究所 情報処理室

Contact Form 7 Add Password field

このプラグインは、Contact Forum 7 プラグインへ password 項目を追加します。
例. [password example] (オプション) / [password* example] (必須)

Contact Form 7 Add Password field

最新

ここでは 2.9 で実装したフックを使った Validation チェックについての紹介をします。

フックの使い方

以下、サポートしているフックを テーマのための関数「functions.php」 内でどのように記述するかの例をあげておきます。 フックを利用すると fucntions.php やプラグインから、本プラグインの Validationチェックを変更、追加できます。$_POST や $_GET 等、フォームデータを取得するときには、無害化(エスケープ)の必要性を常に考えてください。

例1. WordPress のログインユーザーのパスワードと同一かチェックする (2021-07-21)

Contact Form 7 の password フィールドに入力したデータと、とWordPress のログインユーザーのパスワードが一致するかどうかのチェックをします。

[password* password2]

のように設定しているとします。
比較するユーザーは、 sample ユーザーとします。

WordPress のパスワードは、Hash化された保存されますが、WordPress 独自のものと、MD5でハッシュ化されたものなど複数あります。そこは、生パスワードと Hash化されたパスワードのチェック関数「wp_check_password」 が吸収してくれます。

必要なこと
1. 入力したフォームからパスワードデータを取得($_POST[‘password2’])
2. WordPress の sample ユーザーの ハッシュ化されたパスワードとIDを取得

$user_info = get_user_by( 'login', 'sample' );
$user_hash_pass = (string)$user_info->user_pass;
$user_ID = (string)$user_info->ID;
  1. パスワードのチェック
   if( wp_check_password($pass, $user_hash_pass, $user_ID ) === false ){
        // 無効な項目を追加(第二引数は第一引数のフォーム近辺に表示されるメッセージ)
        $result->invalidate( $tag, 'Do not match WordPress user password.' );
    }

テーマの functions.php の記載例

下記の例では、$_POSTで取得したデータを比較しかしていないこと、パスワードは記号も入ることから無害化(エスケープ)はしていない

add_filter('wpcf7_k_password_validation_filter', 'my_wpcf7_validate', 10, 2);

function my_wpcf7_validate($result,$tag){
    $pass = isset( $_POST['password2'] ) ? $_POST['password2']  : '';
    $user_info = get_user_by( 'login', 'sample' );
    $user_hash_pass = (string)$user_info->user_pass;
    $user_ID = (string)$user_info->ID;
    // 何らかの判別
    if( wp_check_password($pass, $user_hash_pass, $user_ID ) === false ){
        // 無効な項目を追加(第二引数は第一引数のフォーム近辺に表示されるメッセージ)
        $result->invalidate( $tag, 'Do not match WordPress user password.' );
    }
    return $result;
}

以下、Contact Form 7を使ったTiPSです。

WordPress へユーザー登録する (2021-07-22)

下記の例は、ユーザー名、パスワード、メールアドレスを Contact Form 7 および本プラグインを使って入力させた上で、送信をおした後に Contact Form 7 の  wpcf7_mail_sent フックを利用して WordPress へユーザー登録する仕組みです。既存ユーザーがいる、登録した場合のエラーメッセージ、その他各種設定は置き換える必要があります。

そのため安易の下記をコピー&ペーストして使わず、必ず設定を見直してください。
*特に安易に設定すると、勝手にWordPressのユーザーを作成できてしまうので注意が必要です。安易なパスワードを設定されないように工夫する(([password* password2 minlength:12 password_strength:4]等)など考えてください。あくまで下記はもっとも単純にしたコード例にすぎないことを覚えておいてください。

以下は、テーマの functions.php に追加することを想定したコードです。

以上の設定でデータを入力してもらったと仮定します。


add_action('wpcf7_mail_sent', 'my_wpcf7_user_register', 10, 1);

function my_wpcf7_user_register(){
    $submission = WPCF7_Submission::get_instance();
    if( ! $submission ){
            return;
    }
    //  $send_to = "your e-mail address";  or the admin e-mail for WordPress in case of the following setting..
    $send_to = get_option('admin_email');
    $pass_field = "password2";      // In case of [password* password2] for the user password field
    $username_field = "your-name";    // In case of [text* your-name] for the username
    $email_field = "your-email";     // In case of [email* your-email] for the email

    $formdata = $submission->get_posted_data();
    $pass = isset( $formdata[$pass_field] ) ? trim($formdata[$pass_field]) : '';
    // Snitize the slashes and remove the newline mark.
    $username = isset( $formdata[$username_field] )
        ? trim( wp_unslash( strtr( (string) $formdata[$username_field], "\n", '' ) ) ) 
        : '';   
    $email = isset( $formdata[$email_field] )
        ? trim( wp_unslash( strtr( (string) $formdata[$email_field], "\n", '' ) ) ) 
        : '';       

    // Set the user information. 
    // About other items, please see https://developer.wordpress.org/reference/functions/wp_insert_user/ . 
    $user_args = array(
            'user_login' => $username,
            'user_pass' => $pass,
            'user_email' => $email,
            'role' => 'contributor', 
            ); 
    // If you already have a WordPress user, give an error.
    if (username_exists($username) !== false){
        /* In case that the user "$username" already exists in WordPress user*/
        wp_mail($send_to, "Cannot register", "The user ".$username." is already registered.");
    }else{
        // User registration
        $create_user = wp_insert_user($user_args);
        if(is_wp_error ($create_user)){
            wp_mail($send_to, "Create user is fault", "username:". $username );
        }else{
            wp_mail($send_to, "Created user", 
                "The following user is resgitered. \n"              
                . "username: ". $username . "\n"
                . "user_email: ". $email . "\n"
            );
        }
    }
}

Contact Form 7 の入力値を変更する (2023-01-28)

以下の例は、パスワードなど値を秘匿したい場合につかうTiPSです。本プラグイン内に組み込むことはできず、テーマの functions.php に追加します。

以上の設定でデータを入力してもらったと仮定します。
*ここでは変更したい項目が2つあり、それぞれ「user-pass」「check-user-pass」とします。また入力された値をハッシュ化しています。パスワードを生データであつかわず、ハッシュ値で保存する場合に利用できるでしょう。
実際に Contact Form 7 では本プラグインを使って、パスワード入力とそれをチェックする入力フォームを作成したとします。

 

そうしておいて、テーマの functions.php に次のコードを追加します。


add_filter('wpcf7_posted_data', 'my_wpcf7_posted_data', 10, 1);
 
function my_wpcf7_posted_data($posted_data){
	if( isset($posted_data['user-pass']) ){
	  	$posted_data['user-pass'] = wp_hash( $posted_data['user-pass'] );
	}			
	if( isset($posted_data['check-user-pass']) ){
	  	$posted_data['check-user-pass'] = wp_hash( $posted_data['check-user-pass'] );
	}			
    return $posted_data;
}

*もし値を入力させずしたい場合には、次のようなコードになるでしょう。


function my_wpcf7_posted_data($posted_data){
	if( isset($posted_data['user-pass']) ){
	  	$posted_data['user-pass'] = "";
	}			
	if( isset($posted_data['check-user-pass']) ){
	  	$posted_data['check-user-pass'] = "";
	}			
    return $posted_data;
}

2021年7月21日作成
2023年1月28日更新

モバイルバージョンを終了