見出し画像

mw wp-formのフォーム作成画面からreadonlyを選択できるようにする

wordpressってあんまり共同開発向きじゃないよね…なんでwordpressを採用したんだろう…と業務で使う中で何度も思いました。

今回はそのwordpressのプラグインMW WP FORM v4を使っていて、デフォルトの機能ではこと足らなくなったので、管理画面に編集項目を増やす方法を書きます。

まずWordpressのプラグインはwordpress\wp-content\pluginsの中に入ります。この中にmw-wp-formというフォルダができているはずなのでこの中のファイルを編集します。今回はWP管理画面のWPFormのメール用テキストフォームタグ作成の時にreadonlyを付与したのでその方法で紹介します。

classes/form-fields/class.email.php

classes/form-fieldsの中身は管理画面での表示とか初期値とか設定できるみたいです。この中のclass.email.phpがメール用フォームに関わる部分なのでこの中身を以下のように編集します。

	protected function set_defaults() {
		return array(
			'name'        => '',
			'id'          => null,
			'class'       => null,
			'size'        => 60,
			'maxlength'   => null,
			'value'       => '',
			'placeholder' => null,
			'readonly'    => null,   //デフォルト値を追加
			'show_error'  => 'true',
			'conv_half_alphanumeric' => 'true',
		);
	}
protected function input_page() {
   …
   $_ret = $this->Form->email( $this->atts['name'], array(
       'id'          => $this->atts['id'],
       'class'       => $this->atts['class'],
       'size'        => $this->atts['size'],
       'maxlength'   => $this->atts['maxlength'],
       'value'       => $value,
       'placeholder' => $this->atts['placeholder'],
       'readonly'    => $this->atts['readonly'],   //値の受け渡しを追加
       'conv-half-alphanumeric' => $conv_half_alphanumeric,
   ) );
   …
}
/* 管理画面への選択項目表示を追加 */
<p>
   <strong>readonly</strong>
   <?php $readonly = $this->get_value_for_generator( 'readonly', $options ); ?>
   <label><input type="checkbox" name="readonly" value="true" <?php checked( 'false', $readonly ); ?> /><?php esc_html_e( 'readonly.', 'mw-wp-form' ); ?></label>
</p>

これで管理画面でreadonlyを選択できるようになります。

キャプチャ

classes\models\class.form.php

モデルの中のclass.form.phpのメールの項目にもreadonlyを追加しましょう。

	/**
	 * Return input[type=email]
	 *
	 * @param string $name
	 * @param array $options
	 * @return string
	 */
	public function email( $name, $options = array() ) {
		$defaults = array(
			'id'          => null,
			'class'       => null,
			'size'        => 60,
			'maxlength'   => null,
			'value'       => '',
			'placeholder' => null,
			'readonly'    => null, //追加
			'conv-half-alphanumeric' => null,
		);

templates\form-fields\email.php

フォームを生成する際に使われるのはtemplatesの中のform-fieldsフォルダのファイルです。フォーム生成画面のメール用フォームで入力した値はこの中のemail.phpファイルを通るのでこちらにreadonlyがチェックされたときも値が渡るよう設定します。

<input type="email"
	name="<?php echo esc_attr( $name ); ?>"
	<?php echo MWF_Functions::generate_input_attribute( 'id', $id ); ?>
	<?php echo MWF_Functions::generate_input_attribute( 'class', $class ); ?>
	<?php echo MWF_Functions::generate_input_attribute( 'size', $size ); ?>
	<?php echo MWF_Functions::generate_input_attribute( 'maxlength', $maxlength ); ?>
	<?php echo MWF_Functions::generate_input_attribute( 'value', $value ); ?>
	<?php echo MWF_Functions::generate_input_attribute( 'placeholder', $placeholder ); ?>
	<?php echo MWF_Functions::generate_input_attribute( 'readonly', $readonly ); ?>  //追加
	<?php echo MWF_Functions::generate_input_attribute( 'data-conv-half-alphanumeric', $conv_half_alphanumeric ); ?>
/>

これでタイプ:メールを選んでフォーム生成するときにreadonlyを付加できるようになります。

参考:http://valentinus57.rssing.com/chan-31465803/latest.php
   [MW WP Form] ショートコードにpattern属性追加


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