お問い合わせフォームの内容を変更できないようにする(readonly属性を追加)wordress contact Form 7
以下の記事を参考にcontact form7でお問い合わせフォームを実装し、記事からお問い合わせページへ任意の内容を引き継ぐ事ができた。
- WordPress › フォーラム » Contact form 7 商品問い合わせボタン
- WordPress › フォーラム » Contact form 7 記事タイトルの自動表示でIEだけ日本語が表示されない
ただ、これだと引き継いだ情報を編集する事ができてしまうので、良くない。
そこでcontact form7を弄り、管理画面からreadonly属性を追加できるようにしてみた。
contact form7の編集
- contact-form-7/modules/text.phpを開き以下の「// 追加」となっている部分を追加する
<?php /** ** A base module for [text], [text*], [email], and [email*] **/ /* Shortcode handler */ wpcf7_add_shortcode( 'text', 'wpcf7_text_shortcode_handler', true ); wpcf7_add_shortcode( 'text*', 'wpcf7_text_shortcode_handler', true ); wpcf7_add_shortcode( 'email', 'wpcf7_text_shortcode_handler', true ); wpcf7_add_shortcode( 'email*', 'wpcf7_text_shortcode_handler', true ); function wpcf7_text_shortcode_handler( $tag ) { if ( ! is_array( $tag ) ) return ''; $type = $tag['type']; $name = $tag['name']; $options = (array) $tag['options']; $values = (array) $tag['values']; if ( empty( $name ) ) return ''; $validation_error = wpcf7_get_validation_error( $name ); $atts = $id_att = $size_att = $maxlength_att = ''; $tabindex_att = $title_att = ''; $readonly_att = ''; // 追加 $class_att = wpcf7_form_controls_class( $type, 'wpcf7-text' ); if ( 'email' == $type || 'email*' == $type ) $class_att .= ' wpcf7-validates-as-email'; if ( $validation_error ) $class_att .= ' wpcf7-not-valid'; foreach ( $options as $option ) { if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) { $id_att = $matches[1]; } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) { $class_att .= ' ' . $matches[1]; } elseif ( preg_match( '%^([0-9]*)[/x]([0-9]*)$%', $option, $matches ) ) { $size_att = (int) $matches[1]; $maxlength_att = (int) $matches[2]; } elseif ( preg_match( '%^readonly$%', $option, $matches ) ) { $readonly_att = true; // 追加 } elseif ( preg_match( '%^tabindex:(¥d+)$%', $option, $matches ) ) { $tabindex_att = (int) $matches[1]; } } $value = (string) reset( $values ); if ( wpcf7_script_is() && preg_grep( '%^watermark$%', $options ) ) { $class_att .= ' wpcf7-use-title-as-watermark'; $title_att .= sprintf( ' %s', $value ); $value = ''; } elseif ( empty( $value ) && is_user_logged_in() ) { $user = wp_get_current_user(); $user_options = array( 'default:user_login' => 'user_login', 'default:user_email' => 'user_email', 'default:user_url' => 'user_url', 'default:user_first_name' => 'first_name', 'default:user_last_name' => 'last_name', 'default:user_nickname' => 'nickname', 'default:user_display_name' => 'display_name' ); foreach ( $user_options as $option => $prop ) { if ( preg_grep( '%^' . $option . '$%', $options ) ) { $value = $user->{$prop}; break; } } } if ( wpcf7_is_posted() && isset( $_POST[$name] ) ) $value = stripslashes_deep( $_POST[$name] ); if ( $readonly_att ) $atts .= ' readonly="readonly"'; // 追加 if ( $id_att ) $atts .= ' id="' . trim( $id_att ) . '"'; if ( $class_att ) $atts .= ' class="' . trim( $class_att ) . '"'; if ( $size_att ) $atts .= ' size="' . $size_att . '"'; else $atts .= ' size="40"'; // default size if ( $maxlength_att ) $atts .= ' maxlength="' . $maxlength_att . '"'; if ( '' !== $tabindex_att ) $atts .= sprintf( ' tabindex="%d"', $tabindex_att ); if ( $title_att ) $atts .= sprintf( ' title="%s"', trim( esc_attr( $title_att ) ) ); $html = '<input type="text" name="' . $name . '" value="' . esc_attr( $value ) . '"' . $atts . ' />'; $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>'; return $html; } 〜略〜
29行目付近
$readonly_att = ''; // 追加
49行目付近
} elseif ( preg_match( '%^readonly$%', $option, $matches ) ) {
$readonly_att = true; // 追加
88行目
if ( $readonly_att )
$atts .= ' readonly="readonly"'; // 追加
これでフォームの管理画面でreadonlyを追加してあげればフォームにreadonly属性が付加される
contact form7の管理画面でフォームの項目にreadonly属性を追加
完成。
ただ、記事から情報を引き継ぐタイプのお問い合わせフォームと、プレーンな問い合わせフォームとを共有している場合はタグを分けるか、フォームを分けるかする必要がある。