wpcf7_select_form_tag_handler

The Contact Form 7 wpcf7 select form tag handler function.

Description

wpcf7_select_form_tag_handler( $tag ); 

Parameters (1)

0. $tag
The tag.

Usage

  1. if ( !function_exists( 'wpcf7_select_form_tag_handler' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'contact-form-7/modules/select.php'; 
  3.  
  4. // The tag. 
  5. $tag = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wpcf7_select_form_tag_handler($tag); 
  9.  

Defined (1)

The function is defined in the following location(s).

/modules/select.php  
  1. function wpcf7_select_form_tag_handler( $tag ) { 
  2. $tag = new WPCF7_FormTag( $tag ); 
  3.  
  4. if ( empty( $tag->name ) ) { 
  5. return ''; 
  6.  
  7. $validation_error = wpcf7_get_validation_error( $tag->name ); 
  8.  
  9. $class = wpcf7_form_controls_class( $tag->type ); 
  10.  
  11. if ( $validation_error ) { 
  12. $class .= ' wpcf7-not-valid'; 
  13.  
  14. $atts = array(); 
  15.  
  16. $atts['class'] = $tag->get_class_option( $class ); 
  17. $atts['id'] = $tag->get_id_option(); 
  18. $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true ); 
  19.  
  20. if ( $tag->is_required() ) { 
  21. $atts['aria-required'] = 'true'; 
  22.  
  23. $atts['aria-invalid'] = $validation_error ? 'true' : 'false'; 
  24.  
  25. $multiple = $tag->has_option( 'multiple' ); 
  26. $include_blank = $tag->has_option( 'include_blank' ); 
  27. $first_as_label = $tag->has_option( 'first_as_label' ); 
  28.  
  29. if ( $tag->has_option( 'size' ) ) { 
  30. $size = $tag->get_option( 'size', 'int', true ); 
  31.  
  32. if ( $size ) { 
  33. $atts['size'] = $size; 
  34. } elseif ( $multiple ) { 
  35. $atts['size'] = 4; 
  36. } else { 
  37. $atts['size'] = 1; 
  38.  
  39. $values = $tag->values; 
  40. $labels = $tag->labels; 
  41.  
  42. if ( $data = (array) $tag->get_data_option() ) { 
  43. $values = array_merge( $values, array_values( $data ) ); 
  44. $labels = array_merge( $labels, array_values( $data ) ); 
  45.  
  46. $defaults = array(); 
  47.  
  48. $default_choice = $tag->get_default_option( null, 'multiple=1' ); 
  49.  
  50. foreach ( $default_choice as $value ) { 
  51. $key = array_search( $value, $values, true ); 
  52.  
  53. if ( false !== $key ) { 
  54. $defaults[] = (int) $key + 1; 
  55.  
  56. if ( $matches = $tag->get_first_match_option( '/^default:([0-9_]+)$/' ) ) { 
  57. $defaults = array_merge( $defaults, explode( '_', $matches[1] ) ); 
  58.  
  59. $defaults = array_unique( $defaults ); 
  60.  
  61. $shifted = false; 
  62.  
  63. if ( $include_blank || empty( $values ) ) { 
  64. array_unshift( $labels, '---' ); 
  65. array_unshift( $values, '' ); 
  66. $shifted = true; 
  67. } elseif ( $first_as_label ) { 
  68. $values[0] = ''; 
  69.  
  70. $html = ''; 
  71. $hangover = wpcf7_get_hangover( $tag->name ); 
  72.  
  73. foreach ( $values as $key => $value ) { 
  74. $selected = false; 
  75.  
  76. if ( $hangover ) { 
  77. if ( $multiple ) { 
  78. $selected = in_array( esc_sql( $value ), (array) $hangover ); 
  79. } else { 
  80. $selected = ( $hangover == esc_sql( $value ) ); 
  81. } else { 
  82. if ( ! $shifted && in_array( (int) $key + 1, (array) $defaults ) ) { 
  83. $selected = true; 
  84. } elseif ( $shifted && in_array( (int) $key, (array) $defaults ) ) { 
  85. $selected = true; 
  86.  
  87. $item_atts = array( 
  88. 'value' => $value,  
  89. 'selected' => $selected ? 'selected' : '' ); 
  90.  
  91. $item_atts = wpcf7_format_atts( $item_atts ); 
  92.  
  93. $label = isset( $labels[$key] ) ? $labels[$key] : $value; 
  94.  
  95. $html .= sprintf( '<option %1$s>%2$s</option>',  
  96. $item_atts, esc_html( $label ) ); 
  97.  
  98. if ( $multiple ) { 
  99. $atts['multiple'] = 'multiple'; 
  100.  
  101. $atts['name'] = $tag->name . ( $multiple ? '[]' : '' ); 
  102.  
  103. $atts = wpcf7_format_atts( $atts ); 
  104.  
  105. $html = sprintf( 
  106. '<span class="wpcf7-form-control-wrap %1$s"><select %2$s>%3$s</select>%4$s</span>',  
  107. sanitize_html_class( $tag->name ), $atts, $html, $validation_error ); 
  108.  
  109. return $html;