bb_option_form_element

Options.

Description

bb_option_form_element( (string) $name = 'name', (null) $args = null ); 

Parameters (2)

0. $name — Optional. (string) => 'name'
The name.
1. $args — Optional. (null) => null
The args.

Usage

  1. if ( !function_exists( 'bb_option_form_element' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-admin/includes/functions.bb-admin.php'; 
  3.  
  4. // The name. 
  5. $name = 'name'; 
  6.  
  7. // The args. 
  8. $args = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bb_option_form_element($name, $args); 
  12.  

Defined (1)

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

/bp-forums/bbpress/bb-admin/includes/functions.bb-admin.php  
  1. function bb_option_form_element( $name = 'name', $args = null ) { 
  2. global $bb_hardcoded; 
  3.  
  4. $defaults = array( 
  5. 'title' => 'title',  
  6. 'type' => 'text',  
  7. 'value' => false,  
  8. 'options' => false,  
  9. 'message' => false,  
  10. 'class' => false,  
  11. 'default' => false,  
  12. 'before' => '',  
  13. 'after' => '',  
  14. 'note' => false,  
  15. 'attributes' => false,  
  16. 'disabled' => false,  
  17. ); 
  18.  
  19. $args = wp_parse_args( $args, $defaults ); 
  20.  
  21. $id = str_replace( array( '_', '[', ']' ), array( '-', '-', '' ), $name ); 
  22. if ( false !== strpos( $name, '[' ) ) { 
  23. list( $option_name, $option_key ) = preg_split( '/[\[\]]/', $name, -1, PREG_SPLIT_NO_EMPTY ); 
  24. $option = bb_get_option( $option_name ); 
  25. $value = false === $args['value'] ? esc_attr( $option[$option_key] ) : esc_attr( $args['value'] ); 
  26. $hardcoded = isset( $bb_hardcoded[$option_name][$option_key] ); 
  27. } else { 
  28. $value = false === $args['value'] ? bb_get_form_option( $name ) : esc_attr( $args['value'] ); 
  29. $hardcoded = isset( $bb_hardcoded[$name] ); 
  30.  
  31. $class = $args['class'] ? (array) $args['class'] : array(); 
  32. array_unshift( $class, $args['type'] ); 
  33.  
  34. if ( $hardcoded || $args['disabled'] ) 
  35. $disabled = ' disabled="disabled"'; 
  36. else 
  37. $disabled = false; 
  38.  
  39. if ( $args['attributes'] ) { 
  40. $attributes = array(); 
  41. foreach ( $args['attributes'] as $k => $v ) 
  42. $attributes[] = "$k='$v'"; 
  43. $attributes = ' ' . join( ' ', $attributes ); 
  44. } else { 
  45. $attributes = ''; 
  46.  
  47. ?> 
  48.  
  49. <div id="option-<?php echo $id; ?>"<?php if ( !empty( $disabled ) ) echo ' class="disabled"'; ?>> 
  50. <?php 
  51. switch ( $args['type'] ) { 
  52. case 'radio' : 
  53. case 'checkbox' : 
  54. case 'message' : 
  55. ?> 
  56. <div class="label"> 
  57. <?php echo $args['title']; ?> 
  58. </div> 
  59.  
  60. <?php 
  61. break; 
  62. case 'select' : 
  63. default : 
  64. ?> 
  65. <label for="<?php echo $id; ?>"> 
  66. <?php echo $args['title']; ?> 
  67. </label> 
  68.  
  69. <?php 
  70. break; 
  71. ?> 
  72. <div class="inputs"> 
  73.  
  74. <?php 
  75. if ( $args['before'] ) { 
  76. echo '<span class="before">' . $args['before'] . '</span>'; 
  77. switch ( $args['type'] ) { 
  78. case 'select' : 
  79. echo "<select$disabled class='" . join( ' ', $class ) . "' name='$name' id='$id'$attributes>\n"; 
  80. if ( is_array( $args['options'] ) ) { 
  81. foreach ( $args['options'] as $option => $label ) 
  82. echo "\t<option value='$option'" . ( $value == $option ? " selected='selected'" : '' ) . ">$label</option>\n"; 
  83. } elseif ( is_string( $args['options'] ) ) { 
  84. echo $args['options'] . "\n"; 
  85. echo "</select>\n"; 
  86. break; 
  87. case 'radio' : 
  88. case 'checkbox' : 
  89. if ( is_array( $args['options'] ) ) { 
  90. $_id = 0; 
  91. if ( 'radio' === $args['type'] && !in_array( $value, array_keys( $args['options'] ) ) && empty( $value ) ) { 
  92. $use_first_value = true; 
  93. $type = $args['type']; 
  94. foreach ( $args['options'] as $option => $label ) { 
  95. if ( $use_first_value ) { 
  96. $use_first_value = false; 
  97. $value = $option; 
  98. if ( is_array( $label ) ) { 
  99. if ( isset( $label['attributes'] ) ) { 
  100. $attributes = array(); 
  101. foreach ( $label['attributes'] as $k => $v ) 
  102. $attributes[] = "$k='$v'"; 
  103. $attributes = ' ' . join( ' ', $attributes ); 
  104. } else { 
  105. $attributes = ''; 
  106. if ( isset( $label['name'] ) ) { 
  107. $name = $label['name']; 
  108. $id = str_replace( array( '_', '[', ']' ), array( '-', '-', '' ), $name ); 
  109. $hardcoded = isset( $bb_hardcoded[$name] ); 
  110. if ( isset( $label['value'] ) ) { 
  111. $_value = $label['value']; 
  112. } else { 
  113. $_value = $args['value']; 
  114. $value = false === $_value ? bb_get_form_option( $name ) : esc_attr( $_value ); 
  115. $label = $label['label']; 
  116. echo "<label class=\"{$type}s\"><input$disabled type='$type' class='" . join( ' ', $class ) . "' name='$name' id='$id-$_id' value='$option'" . ( $value == $option ? " checked='checked'" : '' ) . "{$attributes} /> $label</label>\n"; 
  117. $_id++; 
  118. } elseif ( is_string( $args['options'] ) ) { 
  119. echo $args['options'] . "\n"; 
  120. break; 
  121. case 'message' : 
  122. if ( $args['message'] ) { 
  123. echo $args['message']; 
  124. break; 
  125. default : 
  126. echo "<input$disabled type='$args[type]' class='" . join( ' ', $class ) . "' name='$name' id='$id' value='$value'$attributes />\n"; 
  127. break; 
  128. if ( $args['after'] ) { 
  129. echo '<span class="after">' . $args['after'] . '</span>'; 
  130.  
  131. if ( $args['note'] ) { 
  132. foreach ( (array) $args['note'] as $note ) { 
  133. ?> 
  134.  
  135. <p><?php echo $note; ?></p> 
  136.  
  137. <?php 
  138. ?> 
  139.  
  140. </div> 
  141. </div> 
  142.  
  143. <?php