住所フィールドのサブラベルを変更する方法

WPFormsの住所フィールドのサブラベルを変更したいですか? これらのサブラベルは、ユーザが入力することが期待されている情報の種類をユーザに知らせます。このチュートリアルでは、PHPを使ってこれらのサブラベルを変更する方法を説明します。

デフォルトでは、住所フォームフィールドは追加フィールドを表示します。 これらのフィールドはそれぞれサブラベルと呼ばれる独自のラベルを持っています。

デフォルトでは、住所フォームフィールドは追加フィールドをもたらします。  これらのフィールドはそれぞれサブラベルと呼ばれる独自のラベルを持っています。

フォームの作成

まずフォームを作成し、住所フォームフィールドを含むフィールドを追加します。

フォームの作成にサポートが必要な場合は、こちらのドキュメントをご覧ください

フォームを作成し、フィールドを追加する

アドレスのサブラベルを変更する

いよいよスニペットをサイトに追加します。

スニペットをサイトに追加する場所や方法についてサポートが必要な場合は、こちらのチュートリアルをご覧ください

米国住所スキームのみ

以下のコード・スニペットを使用すると、米国スキームの住所フィールドのサブラベルを変更することができます。

/**
 * Change the sublabels for the Address field for the US Address Scheme.
 *
 * @link https://wpforms.com/developers/how-to-change-the-address-field-sublabels/
 */

function wpf_dev_address_field_properties_usa( $properties, $field, $form_data ) {
  
  // check for address scheme
  if ( $field[ 'scheme' ] === 'us' ){

	  // Change sublabel values
	  $properties[ 'inputs' ][ 'address1' ][ 'sublabel' ][ 'value' ] = __( 'Street Address' );
	  $properties[ 'inputs' ][ 'address2' ][ 'sublabel' ][ 'value' ] = __( 'Apartment #' );
	  $properties[ 'inputs' ][ 'city' ][ 'sublabel' ][ 'value' ] = __( 'City' );
	  $properties[ 'inputs' ][ 'state' ][ 'sublabel' ][ 'value' ] = __( 'State' );
	  $properties[ 'inputs' ][ 'postal' ][ 'sublabel' ][ 'value' ] = __( 'Zip' );
  	}

    return $properties;
}

add_filter( 'wpforms_field_properties_address', 'wpf_dev_address_field_properties_usa', 10, 3 );

国際住所スキームのみ

以下のコード・スニペットで、住所フィールドのサブラベルを国際スキーム用に変更できます。

/**
 * Change the sublabels for the Address field for the International Address Scheme.
 *
 * @link https://wpforms.com/developers/how-to-change-the-address-field-sublabels/
 */

function wpf_dev_address_field_properties_international( $properties, $field, $form_data ) {
  
  // check for address scheme
  if ( $field['scheme'] === 'international' ){

	  // Change sublabel values
	  $properties[ 'inputs' ][ 'address1' ][ 'sublabel' ][ 'value' ] = __( 'Address' );
	  $properties[ 'inputs' ][ 'address2' ][ 'sublabel' ][ 'value' ] = __( 'Apartment / Suite #' );
	  $properties[ 'inputs' ][ 'city' ][ 'sublabel' ][ 'value' ] = __( 'City' );
	  $properties[ 'inputs' ][ 'state' ][ 'sublabel' ][ 'value' ] = __( 'State / Province / Region' );
	  $properties[ 'inputs' ][ 'postal' ][ 'sublabel' ][ 'value' ] = __( 'Postal Code' );
	  $properties[ 'inputs' ][ 'country' ][ 'sublabel' ][ 'value' ] = __( 'Country' );
  	}

    return $properties;
}

add_filter( 'wpforms_field_properties_address', 'wpf_dev_address_field_properties_international', 10, 3 );

住所フィールドのサブラベルを好きなものに変更できるようになりました。

以上です! アドレス・フィールドで使用する追加のアドレス・スキームを作成したいですか? アドレス・フィールドの追加スキームを作成する方法をご覧ください。

リファレンス・フィルター

wpforms_field_properties

よくあるご質問

Q: 1つのフォームだけ変更することはできますか?

A:もちろんです。特定のフォームのサブラベルだけを変更したい場合は、このスニペットを使用してください。フォームIDの見つけ方については、こちらのガイドをご覧ください。


/**
 * Change the sublabels for the Address field for the US Address Scheme.
 *
 * @link https://wpforms.com/developers/how-to-change-the-address-field-sublabels/
 */
 
function wpf_dev_address_field_properties_usa( $properties, $field, $form_data ) {
	
	// Only process this snippet on the form ID 123
    if ( absint( $form_data[ 'id' ] ) !== 123 ) {

        return $properties;
    } 
   
  // check for address scheme
  if ( $field[ 'scheme' ] === 'us' ){
 
      // Change sublabel values
      $properties[ 'inputs' ][ 'address1' ][ 'sublabel' ][ 'value' ] = __( 'Street Address' );
      $properties[ 'inputs' ][ 'address2' ][ 'sublabel' ][ 'value' ] = __( 'Apartment #' );
      $properties[ 'inputs' ][ 'city' ][ 'sublabel' ][ 'value' ] = __( 'City' );
      $properties[ 'inputs' ][ 'state' ][ 'sublabel' ][ 'value' ] = __( 'State' );
      $properties[ 'inputs' ][ 'postal' ][ 'sublabel' ][ 'value' ] = __( 'Zip' );
    }
 
    return $properties;
}
 
add_filter( 'wpforms_field_properties_address' , 'wpf_dev_address_field_properties_usa', 10, 3 );

スニペットでわかるように、フォームIDのチェックを次のように追加するだけである。 if ( absint( $form_data[ 'id' ] ) !== 123 ) { return $properties; } スニペットの残りの部分は、すべてのフォームを使った例とまったく同じです。