以下是我在一个项目中使用的代码:
<?php
function load_color_picker_style() {
wp_enqueue_style( \'wp-color-picker\' );
}
add_action(\'admin_print_scripts-widgets.php\', \'load_color_picker_script\');
add_action(\'admin_print_styles-widgets.php\', \'load_color_picker_style\');
?>
///Javascript
jQuery(document).ready(function($){
function updateColorPickers(){
$(\'#widgets-right .wp-color-picker\').each(function(){
$(this).wpColorPicker({
// you can declare a default color here,
// or in the data-default-color attribute on the input
defaultColor: false,
// a callback to fire whenever the color changes to a valid color
change: function(event, ui){},
// a callback to fire when the input is emptied or an invalid color
clear: function() {},
// hide the color picker controls on load
hide: true,
// show a group of common colors beneath the square
// or, supply an array of colors to customize further
palettes: [\'#ffffff\',\'#000000\',\'#ff7c0b\']
});
});
}
updateColorPickers();
$(document).ajaxSuccess(function(e, xhr, settings) {
if(settings.data.search(\'action=save-widget\') != -1 ) {
$(\'.color-field .wp-picker-container\').remove();
updateColorPickers();
}
});
});
唯一的要求是,要成为颜色选择器的输入必须具有类名,
color-picker
.