将颜色选择器字段添加到类别

时间:2013-09-04 作者:Howdy_McGee

我想在我的类别中添加一个自定义字段,用户可以在其中对其进行颜色编码。我已经添加了这个字段,但我想使用内置的颜色选择器(tinyMCE或farbtastic)为用户提供一种简单的颜色选择方法。虽然我还不知道如何添加功能,但到目前为止,我已经掌握了以下内容:

Category Field Setup

/** Add New Field To Category **/
function extra_category_fields( $tag ) {
    $t_id = $tag->term_id;
    $cat_meta = get_option( "category_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="cat_Image_url"><?php _e(\'Category Image Url\'); ?></label></th>
<td>
<input type="text" name="Cat_meta[bgc]" id="colorinput" size="3" style="width:20%;" value="<?php echo $cat_meta[\'bgc\'] ? $cat_meta[\'bgc\'] : \'#fff\'; ?>" class="my-color-field" />
<div id="colorpicker"></div><br />
            <span class="description"><?php _e(\'Can\'t Think of A Desc Yet, Suggestions?\'); ?></span>
                <br />
        </td>
</tr>
<?php
}
add_action ( \'category_add_form_fields\', \'extra_category_fields\');

/** Save Category Meta **/
function save_extra_category_fileds( $term_id ) {
    if ( isset( $_POST[\'Cat_meta\'] ) ) {
        $t_id = $term_id;
        $cat_meta = get_option( "category_$t_id");
        $cat_keys = array_keys($_POST[\'Cat_meta\']);
            foreach ($cat_keys as $key){
            if (isset($_POST[\'Cat_meta\'][$key])){
                $cat_meta[$key] = $_POST[\'Cat_meta\'][$key];
            }
        }
        //save the option array
        update_option( "category_$t_id", $cat_meta );
    }
}
add_action ( \'edited_category\', \'save_extra_category_fileds\');

Colorpicker Script (Farbtastic) - Doesn\'t Work

/** Enqueue Color Picker **/
function farbtastic_scripts() {
  wp_enqueue_script( \'jQuery\' );
  wp_enqueue_style( \'farbtastic\' );
  wp_enqueue_script( \'farbtastic\' );

  ?>
    <script type="text/javascript">

        jQuery(document).ready(function() {
            jQuery(\'#colorpicker\').hide();
            jQuery(\'#colorpicker\').farbtastic("#colorinput");
            jQuery("#colorinput").click(function(){jQuery(\'#colorpicker\').slideToggle()});
        });

    </script>
  <?php
}
add_action( \'admin_enqueue_scripts\', \'farbtastic_scripts\' );
:: Edit :: 如果让它更简单的话,我有一个“高级自定义字段”插件,它有一个颜色选择器选项。我想看看这是否更容易使用。

1 个回复
最合适的回答,由SO网友:Howdy_McGee 整理而成

随着Meta一词的引入,WordPress 4+的这一点已经更新。代码被大量注释。

:: My Functions - functions.php ::

以下功能用于在“添加新类别”屏幕上显示颜色选择器:

/**
 * Add new colorpicker field to "Add new Category" screen
 * - https://developer.wordpress.org/reference/hooks/taxonomy_add_form_fields/
 *
 * @param String $taxonomy
 *
 * @return void
 */
function colorpicker_field_add_new_category( $taxonomy ) {

  ?>

    <div class="form-field term-colorpicker-wrap">
        <label for="term-colorpicker">Category Color</label>
        <input name="_category_color" value="#ffffff" class="colorpicker" id="term-colorpicker" />
        <p>This is the field description where you can tell the user how the color is used in the theme.</p>
    </div>

  <?php

}
add_action( \'category_add_form_fields\', \'colorpicker_field_add_new_category\' );  // Variable Hook Name
以下功能将把colorpicker字段添加到“Edit Category”(编辑类别)屏幕:

/**
 * Add new colopicker field to "Edit Category" screen
 * - https://developer.wordpress.org/reference/hooks/taxonomy_add_form_fields/
 *
 * @param WP_Term_Object $term
 *
 * @return void
 */
function colorpicker_field_edit_category( $term ) {

    $color = get_term_meta( $term->term_id, \'_category_color\', true );
    $color = ( ! empty( $color ) ) ? "#{$color}" : \'#ffffff\';

  ?>

    <tr class="form-field term-colorpicker-wrap">
        <th scope="row"><label for="term-colorpicker">Severity Color</label></th>
        <td>
            <input name="_category_color" value="<?php echo $color; ?>" class="colorpicker" id="term-colorpicker" />
            <p class="description">This is the field description where you can tell the user how the color is used in the theme.</p>
        </td>
    </tr>

  <?php


}
add_action( \'category_edit_form_fields\', \'colorpicker_field_edit_category\' );   // Variable Hook Name
下面的函数将清理并保存术语meta,而不使用# 但是有一个单独的函数,它将使用名为sanitize_hex_color():

/**
 * Term Metadata - Save Created and Edited Term Metadata
 * - https://developer.wordpress.org/reference/hooks/created_taxonomy/
 * - https://developer.wordpress.org/reference/hooks/edited_taxonomy/
 *
 * @param Integer $term_id
 *
 * @return void
 */
function save_termmeta( $term_id ) {

    // Save term color if possible
    if( isset( $_POST[\'_category_color\'] ) && ! empty( $_POST[\'_category_color\'] ) ) {
        update_term_meta( $term_id, \'_category_color\', sanitize_hex_color_no_hash( $_POST[\'_category_color\'] ) );
    } else {
        delete_term_meta( $term_id, \'_category_color\' );
    }

}
add_action( \'created_category\', \'save_termmeta\' );  // Variable Hook Name
add_action( \'edited_category\',  \'save_termmeta\' );  // Variable Hook Name
现在已经添加了字段并保存了数据,我们需要将放置颜色选择器的脚本排队。请注意,在第一个条件测试中,我将根据分类法显示字段。

/**
 * Enqueue colorpicker styles and scripts.
 * - https://developer.wordpress.org/reference/hooks/admin_enqueue_scripts/
 *
 * @return void
 */
function category_colorpicker_enqueue( $taxonomy ) {

    if( null !== ( $screen = get_current_screen() ) && \'edit-category\' !== $screen->id ) {
        return;
    }

    // Colorpicker Scripts
    wp_enqueue_script( \'wp-color-picker\' );

    // Colorpicker Styles
    wp_enqueue_style( \'wp-color-picker\' );

}
add_action( \'admin_enqueue_scripts\', \'category_colorpicker_enqueue\' );
最后,我们需要初始化colorpicker。在这里,我选择将其打印到页脚中,但将其放入外部文件并像普通脚本一样将其排队,可能会更有益,尤其是从插件的角度来看。

/**
 * Print javascript to initialize the colorpicker
 * - https://developer.wordpress.org/reference/hooks/admin_print_scripts/
 *
 * @return void
 */
function colorpicker_init_inline() {

    if( null !== ( $screen = get_current_screen() ) && \'edit-category\' !== $screen->id ) {
        return;
    }

  ?>

    <script>
        jQuery( document ).ready( function( $ ) {

            $( \'.colorpicker\' ).wpColorPicker();

        } ); // End Document Ready JQuery
    </script>

  <?php

}
add_action( \'admin_print_scripts\', \'colorpicker_init_inline\', 20 );

结束

相关推荐

为什么wp_enQueue_SCRIPT(‘jQuery-Masonry’)不工作?

我正在尝试将masonry sript添加到我的站点,但是wp_enqueue_script(\'jquery-masonry\'); 未添加脚本。相反,我必须使用:wp_register_script(\'jquery_masonry\', includes_url(). \'/js/jquery/jquery.masonry.min.js\', \'jquery\'); wp_enqueue_script(\'jquery_masonry\'); 为什么会这样?