Skip to content

Commit 6be0d6c

Browse files
committed
Merge branch 'kowsar89-master'
2 parents 3e18002 + f54198f commit 6be0d6c

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
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: 29 additions & 12 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 {
@@ -132,6 +132,10 @@ function admin_init() {
132132
'std' => isset( $option['default'] ) ? $option['default'] : '',
133133
'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '',
134134
'type' => $type,
135+
'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : '',
136+
'min' => isset( $option['min'] ) ? $option['min'] : '',
137+
'max' => isset( $option['max'] ) ? $option['max'] : '',
138+
'step' => isset( $option['step'] ) ? $option['step'] : '',
135139
);
136140

137141
add_settings_field( $section . '[' . $option['name'] . ']', $option['label'], array( $this, 'callback_' . $type ), $section, $section, $args );
@@ -166,12 +170,13 @@ public function get_field_description( $args ) {
166170
*/
167171
function callback_text( $args ) {
168172

169-
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
170-
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
171-
$type = isset( $args['type'] ) ? $args['type'] : 'text';
173+
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
174+
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
175+
$type = isset( $args['type'] ) ? $args['type'] : 'text';
176+
$placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
172177

173-
$html = sprintf( '<input type="%1$s" class="%2$s-text" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"/>', $type, $size, $args['section'], $args['id'], $value );
174-
$html .= $this->get_field_description( $args );
178+
$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 );
179+
$html .= $this->get_field_description( $args );
175180

176181
echo $html;
177182
}
@@ -191,7 +196,18 @@ function callback_url( $args ) {
191196
* @param array $args settings field args
192197
*/
193198
function callback_number( $args ) {
194-
$this->callback_text( $args );
199+
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
200+
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
201+
$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['max'] ) ? '' : ' step="' . $args['step'] . '"';
206+
207+
$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 );
208+
$html .= $this->get_field_description( $args );
209+
210+
echo $html;
195211
}
196212

197213
/**
@@ -286,11 +302,12 @@ function callback_select( $args ) {
286302
*/
287303
function callback_textarea( $args ) {
288304

289-
$value = esc_textarea( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
290-
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
305+
$value = esc_textarea( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
306+
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
307+
$placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="'.$args['placeholder'].'"';
291308

292-
$html = sprintf( '<textarea rows="5" cols="55" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]">%4$s</textarea>', $size, $args['section'], $args['id'], $value );
293-
$html .= $this->get_field_description( $args );
309+
$html = sprintf( '<textarea rows="5" cols="55" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]"%4$s>%5$s</textarea>', $size, $args['section'], $args['id'], $placeholder, $value );
310+
$html .= $this->get_field_description( $args );
294311

295312
echo $html;
296313
}

0 commit comments

Comments
 (0)