我正在尝试创建一个插件,它基本上只是一个WordPress小部件。当我的小部件第一次添加到侧栏时,我很难让WordPress颜色选择器正常工作。我跟踪了这些instructions for integrating the picker. 然而,当第一次添加小部件时,这会导致两个颜色选择器显示在侧栏中,其中只有一个可用。保存设置后,将删除第二个颜色选择器,一切正常。
我还尝试在ajaxComplete
事件激发而不是ready
, 但这会产生相同的结果-两个颜色选择器。
然后我发现this WordPress bug 已标记为无效的。当我尝试使用推荐的方法时sortstop
事件中,我得到了更糟糕的结果-重复的颜色选择器,但没有一个真正起作用。
我正在寻找这个问题的解决方案,因为我已经走到了死胡同,无法发布这个带有已知bug的插件。是否有可能在首次添加小部件时强制保存?还是有其他我没有考虑过的解决这个问题的方法?
Thx!
更新-以下是与颜色选择器相关的代码:
function load_color_picker_style() {
wp_enqueue_style(\'wp-color-picker\');
}
function load_color_picker_script() {
wp_enqueue_script(\'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\');
function form( $instance ) {
$defaults = array(
\'text_color\' => \'#000\',
);
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<script>
//This shows two color pickers. So does using ajaxComplete or sortstop.
jQuery(document).ready(function($) {
$(\'.color-picker\').wpColorPicker();
});
</script>
<p>
<label for="<?php echo $this->get_field_id( \'text_color\' ); ?>"><?php _e( \'Text Color\', \'date-time\' ) ?>:</label>
<input id="<?php echo $this->get_field_id( \'text_color\' ); ?>" name="<?php echo $this->get_field_name( \'text_color\' ); ?>" type="text" value="<?php echo esc_attr( $instance[ \'text_color\' ] ); ?>" class="color-picker" />
</p>
<?php
}