Volete cambiare le sottoetichette del campo Indirizzo in WPForms? Queste sottoetichette consentono agli utenti di sapere che tipo di informazioni devono essere inserite. In questo tutorial, vi spiegheremo come modificare queste sottoetichette utilizzando PHP.
Per impostazione predefinita, il campo del modulo Indirizzo visualizza altri campi. Ciascuno di questi campi ha le proprie etichette, chiamate sottoetichette.
Creazione del modulo
Inizieremo creando il modulo e aggiungendo i nostri campi, tra cui il campo del modulo Indirizzo.
Se avete bisogno di assistenza per creare il vostro modulo, consultate questa documentazione.
Modifica delle sottoetichette degli indirizzi
Ora è il momento di aggiungere lo snippet al vostro sito.
Se avete bisogno di assistenza su dove e come aggiungere gli snippet al vostro sito, consultate questo tutorial.
Solo schema di indirizzi USA
Il seguente frammento di codice consente di modificare le sottoetichette del campo Indirizzo per lo schema USA.
/** * 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 );
Solo schema di indirizzi internazionali
Il seguente frammento di codice consente di modificare le sottoetichette del campo Indirizzo per lo schema Internazionale.
/** * 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 );
E questo è tutto! Volete creare altri schemi di indirizzi da utilizzare nel campo Indirizzo? Date un'occhiata al nostro articolo su Come creare schemi aggiuntivi per il campo Indirizzo.
Filtro di riferimento
FAQ
D: Posso modificarli per un solo modulo?
R: Assolutamente sì, se si desidera modificare queste sottoetichette solo per un particolare modulo, utilizzare questo snippet e ricordarsi di aggiornare l'ID 123 del modulo per farlo corrispondere al proprio ID. Se avete bisogno di aiuto per trovare il vostro ID modulo, consultate questa utile guida.
/** * 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 );
Come si può vedere nello snippet, tutto ciò che occorre fare è aggiungere il controllo per l'ID del modulo con if ( absint( $form_data[ 'id' ] ) !== 123 ) { return $properties; }
Il resto dello snippet rimane identico all'esempio con tutti i moduli.