douqiao5440 2013-08-19 16:22
浏览 51
已采纳

在帖子选项中使用wordpress颜色选择器

I create post options and I want to implement wordpress color picker core inside it

I tried this code I got it from many tutorials and sources but unfortunately It's not working at all, It's like I never added the code.

HTML

<input name="mv_cr_section_color" type="text" id="mv_cr_section_color" value="#ffffff" data-default-color="#ffffff">


PHP

function Colorpicker(){ 
  wp_enqueue_style( 'wp-color-picker');
  wp_enqueue_script( 'wp-color-picker');
}
add_action('admin_enqueue_scripts', 'Colorpicker');


JQuery

jQuery(document).ready(function(){
   jQuery('#mv_cr_section_color').wpColorPicker();
});
  • 写回答

1条回答 默认 最新

  • doudao9915 2013-08-19 20:52
    关注

    You don't say how you're creating the Theme Options page, but the following is a working example. It's almost the same code as your sample code, but the enqueue is done directly on the custom menu page callback and jQuery is being referenced as $ (note its declaration in ready(function($)):

    <?php
    /**
     * Plugin Name: Testing the Color Picker
     */
    
    add_action( 'admin_menu', 'b5f_demo_menu' );
    
    function b5f_demo_menu() 
    {
        add_menu_page(
            'Test', 
            'Test', 
            'edit_pages', 
            'test-slug', 
            'b5f_callback_function'
        );
    }
    
    function b5f_callback_function() 
    {
        wp_enqueue_script('wp-color-picker');
        wp_enqueue_style( 'wp-color-picker' );
        ?>
        <input name="mv_cr_section_color" type="text" id="mv_cr_section_color" value="#ffffff" data-default-color="#ffffff">
        <script type="text/javascript">
        jQuery(document).ready(function($) {   
            $('#mv_cr_section_color').wpColorPicker();
        });             
        </script>
        <?php
    }
    

    When using admin_enqueue_scripts, the callback function has one parameter $hook_suffix. With it, you can make sure the scripts and styles are only added in the correct screen:

    add_action( 'admin_enqueue_scripts', 'b5f_custom_enqueue' );
    
    function b5f_custom_enqueue( $hook_suffix )
    {
        // CHECK IF CORRECT PAGE, IF NOT DO NOTHING
        # if ( 'my_hook-name' != $hook_suffix )
        #    return;
    
        ?>
        <script type="text/javascript">
            // Use this to check the hook_suffix name
            console.log('<?php echo $hook_suffix; ?>');
        </script>
        <?php
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程