Skip to content

Commit f54198f

Browse files
committed
Fixed the step bug in number field and added placeholder and step example
1 parent e2531b1 commit f54198f

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

example/oop-example.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,29 @@ function get_settings_fields() {
5757
'name' => 'text_val',
5858
'label' => __( 'Text Input', 'wedevs' ),
5959
'desc' => __( 'Text input description', 'wedevs' ),
60+
'placeholder' => __( 'Text Input placeholder', 'wedevs' ),
6061
'type' => 'text',
6162
'default' => 'Title',
62-
'sanitize_callback' => 'intval'
63+
'sanitize_callback' => 'sanitize_text_field'
6364
),
6465
array(
6566
'name' => 'number_input',
6667
'label' => __( 'Number Input', 'wedevs' ),
67-
'desc' => __( 'Number field with validation callback `intval`', 'wedevs' ),
68+
'desc' => __( 'Number field with validation callback `floatval`', 'wedevs' ),
69+
'placeholder' => __( '1.99', 'wedevs' ),
70+
'min' => 0,
71+
'max' => 100,
72+
'step' => '0.01',
6873
'type' => 'number',
6974
'default' => 'Title',
70-
'sanitize_callback' => 'intval'
75+
'sanitize_callback' => 'floatval'
7176
),
7277
array(
73-
'name' => 'textarea',
74-
'label' => __( 'Textarea Input', 'wedevs' ),
75-
'desc' => __( 'Textarea description', 'wedevs' ),
76-
'type' => 'textarea'
78+
'name' => 'textarea',
79+
'label' => __( 'Textarea Input', 'wedevs' ),
80+
'desc' => __( 'Textarea description', 'wedevs' ),
81+
'placeholder' => __( 'Textarea placeholder', 'wedevs' ),
82+
'type' => 'textarea'
7783
),
7884
array(
7985
'name' => 'checkbox',

src/class.settings-api.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ function admin_init() {
104104
}
105105

106106
if ( isset($section['desc']) && !empty($section['desc']) ) {
107-
$section['desc'] = '<div class="inside">'.$section['desc'].'</div>';
108-
$callback = create_function('', 'echo "'.str_replace('"', '\"', $section['desc']).'";');
107+
$section['desc'] = '<div class="inside">' . $section['desc'] . '</div>';
108+
$callback = create_function('', 'echo "' . str_replace( '"', '\"', $section['desc'] ) . '";');
109109
} else if ( isset( $section['callback'] ) ) {
110110
$callback = $section['callback'];
111111
} else {
@@ -173,7 +173,7 @@ function callback_text( $args ) {
173173
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
174174
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
175175
$type = isset( $args['type'] ) ? $args['type'] : 'text';
176-
$placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="'.$args['placeholder'].'"';
176+
$placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
177177

178178
$html = sprintf( '<input type="%1$s" class="%2$s-text" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"%6$s/>', $type, $size, $args['section'], $args['id'], $value, $placeholder );
179179
$html .= $this->get_field_description( $args );
@@ -199,10 +199,10 @@ function callback_number( $args ) {
199199
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
200200
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
201201
$type = isset( $args['type'] ) ? $args['type'] : 'number';
202-
$placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="'.$args['placeholder'].'"';
203-
$min = empty( $args['min'] ) ? '' : ' min="'.$args['min'].'"';
204-
$max = empty( $args['max'] ) ? '' : ' max="'.$args['max'].'"';
205-
$step = empty( $args['step'] ) ? '' : ' step="1"';
202+
$placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
203+
$min = empty( $args['min'] ) ? '' : ' min="' . $args['min'] . '"';
204+
$max = empty( $args['max'] ) ? '' : ' max="' . $args['max'] . '"';
205+
$step = empty( $args['max'] ) ? '' : ' step="' . $args['step'] . '"';
206206

207207
$html = sprintf( '<input type="%1$s" class="%2$s-number" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"%6$s%7$s%8$s%9$s/>', $type, $size, $args['section'], $args['id'], $value, $placeholder, $min, $max, $step );
208208
$html .= $this->get_field_description( $args );

0 commit comments

Comments
 (0)