yit_add_select2_fields

Add select 2.

Description

yit_add_select2_fields( (array) $args = array() ); 

Parameters (1)

0. $args — Optional. (array) => array()
The args.

Usage

  1. if ( !function_exists( 'yit_add_select2_fields' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'yith-woocommerce-wishlist/plugin-fw/yit-woocommerce-compatibility.php'; 
  3.  
  4. // The args. 
  5. $args = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = yit_add_select2_fields($args); 
  9.  

Defined (1)

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

/plugin-fw/yit-woocommerce-compatibility.php  
  1. function yit_add_select2_fields( $args = array() ) { 
  2. $default = array( 
  3. 'type' => 'hidden',  
  4. 'class' => '',  
  5. 'id' => '',  
  6. 'name' => '',  
  7. 'data-placeholder' => '',  
  8. 'data-allow_clear' => false,  
  9. 'data-selected' => '',  
  10. 'data-multiple' => false,  
  11. 'data-action' => '',  
  12. 'value' => '',  
  13. 'style' => '',  
  14. 'custom-attributes' => array() 
  15. ); 
  16.  
  17. $args = wp_parse_args( $args, $default ); 
  18.  
  19. $custom_attributes = array(); 
  20. foreach ( $args[ 'custom-attributes' ] as $attribute => $attribute_value ) { 
  21. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"'; 
  22. $custom_attributes = implode( ' ', $custom_attributes ); 
  23.  
  24. if ( version_compare( WC()->version, '2.7.0', '>=' ) ) : 
  25. if ( $args[ 'data-multiple' ] === true && substr( $args[ 'name' ], -2 ) != '[]' ) { 
  26. $args[ 'name' ] = $args[ 'name' ] . '[]'; 
  27. ?> 
  28.  
  29. <select 
  30. id="<?php echo $args[ 'id' ] ?>" 
  31. class="<?php echo $args[ 'class' ] ?>" 
  32. name="<?php echo $args[ 'name' ] ?>" 
  33. data-placeholder="<?php echo $args[ 'data-placeholder' ] ?>" 
  34. data-allow_clear="<?php echo $args[ 'data-allow_clear' ] ?>" 
  35. <?php echo !empty( $args[ 'data-action' ] ) ? 'data-action="' . $args[ 'data-action' ] . '"' : ''; ?> 
  36. <?php echo !empty( $args[ 'data-multiple' ] ) ? 'multiple="multiple"' : ''; ?> 
  37. style="<?php echo $args[ 'style' ] ?>" 
  38. <?php echo $custom_attributes ?> 
  39.  
  40. <?php if ( !empty( $args[ 'value' ] ) ) { 
  41. $values = $args[ 'value' ]; 
  42.  
  43. if ( !is_array( $values ) ) { 
  44. $values = explode( ', ', $values ); 
  45.  
  46. foreach ( $values as $value ): ?> 
  47. <option value="<?php echo $value; ?>" <?php selected( true, true, true ) ?> > 
  48. <?php echo $args[ 'data-selected' ][ $value ]; ?> 
  49. </option> 
  50. <?php endforeach; 
  51. ?> 
  52. </select> 
  53. <?php 
  54. else : 
  55. if ( $args[ 'data-multiple' ] === false && is_array( $args[ 'data-selected' ] ) ) { 
  56. $args[ 'data-selected' ] = current( $args[ 'data-selected' ] ); 
  57.  
  58. ?> 
  59. <input 
  60. type="hidden" 
  61. id="<?php echo $args[ 'id' ] ?>" 
  62. class="<?php echo $args[ 'class' ] ?>" 
  63. name="<?php echo $args[ 'name' ] ?>" 
  64. data-placeholder="<?php echo $args[ 'data-placeholder' ] ?>" 
  65. data-allow_clear="<?php echo $args[ 'data-allow_clear' ] ?>" 
  66. data-selected="<?php echo is_array( $args[ 'data-selected' ] ) ? esc_attr( json_encode( $args[ 'data-selected' ] ) ) : esc_attr( $args[ 'data-selected' ] ) ?>" 
  67. data-multiple="<?php echo $args[ 'data-multiple' ] === true ? 'true' : 'false' ?>" 
  68. <?php echo( !empty( $args[ 'data-action' ] ) ? 'data-action="' . $args[ 'data-action' ] . '"' : '' ) ?> 
  69. value="<?php echo $args[ 'value' ] ?>" 
  70. style="<?php echo $args[ 'style' ] ?>" 
  71. <?php echo $custom_attributes ?> 
  72. /> 
  73. <?php 
  74. endif;