Skip to content

Commit fd6cf14

Browse files
committed
Merge pull request #16 from candypaint/master
Color Picker Support
2 parents 92d7a58 + 43da3ea commit fd6cf14

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

class.settings-api.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __construct() {
3939
* Enqueue scripts and styles
4040
*/
4141
function admin_enqueue_scripts() {
42+
wp_enqueue_style( 'wp-color-picker' );
4243
wp_enqueue_script( 'jquery' );
4344
wp_enqueue_script( 'media-upload' );
4445
wp_enqueue_script( 'thickbox' );
@@ -329,6 +330,22 @@ function callback_password( $args ) {
329330
echo $html;
330331
}
331332

333+
/**
334+
* Displays a color picker field for a settings field
335+
*
336+
* @param array $args settings field args
337+
*/
338+
function callback_color( $args ) {
339+
340+
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
341+
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
342+
343+
$html = sprintf( '<input type="text" class="%1$s-text wp-color-picker-field" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
344+
$html .= sprintf( '<span class="description" style="display:block;"> %s</span>', $args['desc'] );
345+
346+
echo $html;
347+
}
348+
332349
/**
333350
* Sanitize callback for Settings API
334351
*/
@@ -440,14 +457,16 @@ function show_forms() {
440457
}
441458

442459
/**
443-
* Tabbable JavaScript codes
460+
* Tabbable JavaScript codes & Initiate Color Picker
444461
*
445462
* This code uses localstorage for displaying active tabs
446463
*/
447464
function script() {
448465
?>
449466
<script>
450467
jQuery(document).ready(function($) {
468+
//Initiate Color Picker
469+
$('.wp-color-picker-field').wpColorPicker();
451470
// Switches option sections
452471
$('.group').hide();
453472
var activetab = '';

procedural-example.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ function wedevs_admin_init() {
9696
'desc' => __( 'File description', 'wedevs' ),
9797
'type' => 'file',
9898
'default' => ''
99+
),
100+
array(
101+
'name' => 'color',
102+
'label' => __( 'Color', 'wedevs' ),
103+
'desc' => __( 'Color description', 'wedevs' ),
104+
'type' => 'color',
105+
'default' => ''
99106
)
100107
),
101108
'wedevs_advanced' => array(
@@ -165,6 +172,13 @@ function wedevs_admin_init() {
165172
'desc' => __( 'File description', 'wedevs' ),
166173
'type' => 'file',
167174
'default' => ''
175+
),
176+
array(
177+
'name' => 'color',
178+
'label' => __( 'Color', 'wedevs' ),
179+
'desc' => __( 'Color description', 'wedevs' ),
180+
'type' => 'color',
181+
'default' => ''
168182
)
169183
),
170184
'wedevs_others' => array(
@@ -232,6 +246,13 @@ function wedevs_admin_init() {
232246
'desc' => __( 'File description', 'wedevs' ),
233247
'type' => 'file',
234248
'default' => ''
249+
),
250+
array(
251+
'name' => 'color',
252+
'label' => __( 'Color', 'wedevs' ),
253+
'desc' => __( 'Color description', 'wedevs' ),
254+
'type' => 'color',
255+
'default' => ''
235256
)
236257
)
237258
);
@@ -273,4 +294,4 @@ function wedevs_plugin_page() {
273294

274295
echo '</div>';
275296
}
276-
endif;
297+
endif;

0 commit comments

Comments
 (0)