Created
February 16, 2019 16:35
-
-
Save DevWael/3c84f15708dabfc3b46ce144cf910aaf to your computer and use it in GitHub Desktop.
generate form inputs (untested)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'add_item_form_html', 'dw_add_item_form' ); | |
function dw_add_item_form() { | |
$args = [ | |
[ | |
'type' => 'wrapper_start', | |
'class' => 'row', | |
], | |
[ | |
'type' => 'wrapper_end' | |
], | |
[ | |
'type' => 'radio', | |
'name' => 'input_nddame', | |
'label' => 'Select Genger', | |
'class' => 'holder', | |
'value' => 'female', | |
'options' => [ | |
'male' => 'Male', | |
'female' => 'Female', | |
], | |
'wrapper_start' => '', | |
'wrapper_end' => '', | |
], | |
[ | |
'type' => 'text', | |
'name' => 'input_name', | |
'placeholder' => 'holder', | |
'label' => 'holder', | |
'id' => 'holderb', | |
'value' => 'holdern', | |
'class' => 'holder', | |
'wrapper_start' => '', | |
'wrapper_end' => '', | |
], | |
[ | |
'type' => 'select', | |
'name' => 'input_name', | |
'label' => 'holder', | |
'id' => 'holds', | |
'value' => 'opt_key4', | |
'class' => 'holder', | |
'options' => [ | |
'opt_key1' => 'Option Key 1', | |
'opt_key2' => 'Option Key 2', | |
'opt_key3' => 'Option Key 3', | |
'opt_key4' => 'Option Key 4', | |
], | |
'wrapper_start' => '', | |
'wrapper_end' => '', | |
], | |
[ | |
'type' => 'multi_select', | |
'name' => 'input_ddname', | |
'placeholder' => 'holder', | |
'label' => 'holder', | |
'id' => 'hosdlds', | |
'value' => [ 'opt_key1', 'opt_key2' ], | |
'class' => 'holder', | |
'options' => [ | |
'opt_key1' => 'Option Key 1', | |
'opt_key2' => 'Option Key 2', | |
'opt_key3' => 'Option Key 3', | |
'opt_key4' => 'Option Key 4', | |
], | |
'wrapper_start' => '', | |
'wrapper_end' => '', | |
], | |
]; | |
?> | |
<div class="form-content"> | |
<?php | |
foreach ( $args as $arg ) { | |
dw_form_inputs_builder( $arg ); | |
} | |
?> | |
</div> | |
<?php | |
} | |
function dw_form_inputs_builder( $arg, $echo = true ) { | |
$output = $class = $id = $name = $placeholder = $wrapper_start = $wrapper_end = ''; | |
if ( isset( $arg['wrapper_start'] ) ) { | |
if ( ! empty( $arg['wrapper_start'] ) ) { | |
$output .= $arg['wrapper_start']; | |
} else { | |
$output .= '<div class="form-group">'; | |
} | |
} | |
switch ( $arg['type'] ) { | |
case 'wrapper_start': | |
if ( isset( $arg['class'] ) ) { | |
$class = 'class="' . esc_attr( $arg['class'] ) . '"'; | |
} | |
if ( isset( $arg['id'] ) ) { | |
$id = 'id="' . esc_attr( $arg['id'] ) . '"'; | |
} | |
$output .= "<div $class $id>"; | |
break; | |
case 'wrapper_end': | |
$output .= "</div>"; | |
break; | |
case 'text': | |
if ( $arg['label'] ) { | |
$output .= sprintf( '<label for="%s">%s', | |
$arg['id'], | |
$arg['label'] | |
); | |
} | |
if ( $arg['label'] ) { | |
$output .= '</label>'; | |
} | |
$output .= sprintf( '<input name="%s" id="%s" class="form-control %s" type="text" placeholder="%s" value="%s" />', | |
esc_attr( $arg['name'] ), | |
esc_attr( $arg['id'] ), | |
esc_attr( $arg['class'] ), | |
esc_attr( $arg['placeholder'] ), | |
esc_attr( $arg['value'] ) | |
); | |
break; | |
case 'password': | |
if ( $arg['label'] ) { | |
$output .= sprintf( '<label for="%s">%s', | |
$arg['id'], | |
$arg['label'] | |
); | |
} | |
if ( $arg['label'] ) { | |
$output .= '</label>'; | |
} | |
$output .= sprintf( '<input name="%s" id="%s" class="form-control %s" type="password" placeholder="%s" value="%s" />', | |
esc_attr( $arg['name'] ), | |
esc_attr( $arg['id'] ), | |
esc_attr( $arg['class'] ), | |
esc_attr( $arg['placeholder'] ), | |
esc_attr( $arg['value'] ) | |
); | |
break; | |
case 'hidden': | |
$output .= sprintf( '<input name="%s" type="hidden" value="%s"/>', | |
esc_attr( $arg['name'] ), | |
esc_attr( $arg['value'] ) | |
); | |
break; | |
case 'select': | |
if ( $arg['label'] ) { | |
$output .= sprintf( '<label for="%s">%s', | |
$arg['id'], | |
$arg['label'] | |
); | |
} | |
if ( $arg['label'] ) { | |
$output .= '</label>'; | |
} | |
$output .= sprintf( '<select name="%s" id="%s" class="form-control %s">', | |
esc_attr( $arg['name'] ), | |
esc_attr( $arg['id'] ), | |
esc_attr( $arg['class'] ) | |
); | |
$attr = ''; | |
$options = ''; | |
foreach ( $arg['options'] as $key => $label ) { | |
$options .= sprintf( '<option value="%s" %s>%s</option>', | |
$key, | |
selected( $arg['value'], $key, false ), | |
$label | |
); | |
} | |
$output .= $options; | |
$output .= '</select>'; | |
break; | |
case 'multi_select': | |
if ( $arg['label'] ) { | |
$output .= sprintf( '<label for="%s">%s', | |
$arg['id'], | |
$arg['label'] | |
); | |
} | |
if ( $arg['label'] ) { | |
$output .= '</label>'; | |
} | |
$output .= sprintf( '<select name="%s" id="%s" class="form-control %s" multiple>', | |
esc_attr( $arg['name'] ), | |
esc_attr( $arg['id'] ), | |
esc_attr( $arg['class'] ) | |
); | |
$attr = ''; | |
$options = ''; | |
foreach ( $arg['options'] as $key => $label ) { | |
$options .= sprintf( '<option value="%s" %s>%s</option>', | |
$key, | |
selected( ( in_array( $key, $arg['value'] ) ? $key : '' ), $key, false ), | |
$label | |
); | |
} | |
$output .= $options; | |
$output .= '</select>'; | |
break; | |
case 'radio': | |
$options = ''; | |
if ( $arg['label'] ) { | |
$options .= sprintf( '<label>%s</label>', | |
$arg['label'] | |
); | |
} | |
$options .= '<div class="form-group">'; | |
$options .= '<div class="btn-group btn-group-toggle" data-toggle="buttons">'; | |
foreach ( $arg['options'] as $key => $label ) { | |
$options .= sprintf( '<label class="btn btn-outline-dark %s">', | |
checked( $arg['value'], $key, false ) ? 'active' : '' | |
); | |
$options .= sprintf( '<input type="radio" class="form-control" id="%s" name="%s" value="%s" %s>%s', | |
$key, | |
$arg['name'], | |
$key, | |
checked( $arg['value'], $key, false ), | |
$label | |
); | |
$options .= '</label>'; | |
} | |
$options .= '</div>'; | |
$options .= '</div>'; | |
$output .= $options; | |
break; | |
} | |
if ( isset( $arg['wrapper_end'] ) ) { | |
if ( ! empty( $arg['wrapper_end'] ) ) { | |
$output .= $arg['wrapper_end']; | |
} else { | |
$output .= '</div>'; | |
} | |
} | |
if ( $echo ) { | |
echo $output; | |
} else { | |
return $output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment