Guest User

Elementor Forms Code

a guest
Sep 30th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | Source Code | 0 0
  1. add_action( 'elementor_pro/forms/process', function( $record, $ajax_handler ) {
  2.  
  3.   $form_settings = $record->get( 'form_settings' );
  4.   $fields        = $record->get( 'fields' );
  5.  
  6.   if ( empty( $form_settings['form_fields'] ) || empty( $fields ) ) {
  7.     return;
  8.   }
  9.  
  10.   // Pro snadnější dohledání nastavení pole si připravíme mapu: field_id => settings
  11.   $settings_map = [];
  12.   foreach ( $form_settings['form_fields'] as $f ) {
  13.     $fid = isset( $f['custom_id'] ) && $f['custom_id'] !== '' ? $f['custom_id'] : $f['_id'];
  14.     $settings_map[ $fid ] = $f;
  15.   }
  16.  
  17.   foreach ( $fields as $field_id => $field ) {
  18.     // Zajímá nás jen checkbox, select a radio
  19.     if ( ! isset( $field['type'] ) ) continue;
  20.     if ( ! in_array( $field['type'], [ 'checkbox', 'select', 'radio' ], true ) ) continue;
  21.  
  22.     // Najdi odpovídající nastavení pole
  23.     if ( ! isset( $settings_map[ $field['id'] ] ) ) continue;
  24.     $current_settings = $settings_map[ $field['id'] ];
  25.  
  26.     // Z pole "Options" slož mapu value => label
  27.     if ( empty( $current_settings['field_options'] ) ) continue;
  28.  
  29.     $options_lines = preg_split( "/\r\n|\r|\n/", $current_settings['field_options'] );
  30.     if ( ! $options_lines ) continue;
  31.  
  32.     $value_to_label = [];
  33.     foreach ( $options_lines as $line ) {
  34.       $line = trim( $line );
  35.       if ( $line === '' ) continue;
  36.  
  37.       if ( strpos( $line, '|' ) === false ) {
  38.         // Pokud není pipe, ber label == value
  39.         $label = $line;
  40.         $value = $line;
  41.       } else {
  42.         list( $label, $value ) = array_map( 'trim', explode( '|', $line, 2 ) );
  43.       }
  44.       // Prosákne i numerické stringy jako "1500"
  45.       $value_to_label[ (string) $value ] = $label;
  46.     }
  47.  
  48.     // Převod raw_value na label nebo seznam labelů
  49.     $raw = isset( $field['raw_value'] ) ? $field['raw_value'] : $field['value'];
  50.  
  51.     if ( is_array( $raw ) ) {
  52.       // Checkboxy vrací pole
  53.       $labels = [];
  54.       foreach ( $raw as $v ) {
  55.         $v = (string) $v;
  56.         $labels[] = isset( $value_to_label[ $v ] ) ? $value_to_label[ $v ] : $v;
  57.       }
  58.       $new_value = implode( ', ', $labels ); // klidně změň na "\n" pokud chceš každý na nový řádek
  59.     } else {
  60.       $v = (string) $raw;
  61.       $new_value = isset( $value_to_label[ $v ] ) ? $value_to_label[ $v ] : $v;
  62.     }
  63.  
  64.     // Přepiš hodnotu pole, kterou Elementor používá v e-mailech a záznamech
  65.     $record->update_field( $field['id'], 'value', $new_value );
  66.   }
  67.  
  68. }, 20, 2 );
Advertisement
Add Comment
Please, Sign In to add comment