當(dāng)前位置:首頁(yè)>WordPress建站>WordPress開(kāi)發(fā)>WordPress自定義古騰堡編輯器的顏色調(diào)色板

WordPress自定義古騰堡編輯器的顏色調(diào)色板

古騰堡Gutenberg編輯器可以為段落等區(qū)塊設(shè)置文本顏色和背景色等,而且調(diào)色板中提供了12種默認(rèn)顏色:

但是這些顏色可能不一定適配我們的主題,雖然也支持自定義設(shè)置,但是如果我們將這些默認(rèn)顏色選項(xiàng)統(tǒng)一修改了,用戶(hù)只需直接點(diǎn)擊即可應(yīng)用,對(duì)用戶(hù)來(lái)說(shuō)那就極為便利了。

自定義古騰堡顏色調(diào)色板

要實(shí)現(xiàn)上圖右邊的自定義配色方案,可以使用下面的代碼:

<?php
/**
 * change_gutenberg_color_palette.
 *
 * Add theme support for editor-color-palette,
 * and set colours for the gutenberg colour pallete.
 * 
 * @see https://since1979.dev/wp-snippet-002-changing-the-gutenberg-color-palette/
 * 
 * @uses add_theme_support() https://developer.wordpress.org/reference/functions/add_theme_support/
 * @uses __() https://developer.wordpress.org/reference/functions/__/
 * @uses array() https://www.php.net/manual/en/function.array.php
 * 
 * @return void
 */
function change_gutenberg_color_palette() {
 
    add_theme_support( 'editor-color-palette', array(
        array(
            'name' => __('Blackish', 'your-textdomain'),
            'slug' => 'blackish',
            'color' => '#323232',
        ),
        array(
            'name' => __('Whiteish', 'your-textdomain'),
            'slug' => 'white',
            'color' => '#eeeeee',
        ),
        array(
            'name' => __('White', 'your-textdomain'),
            'slug' => 'white',
            'color' => '#ffffff',
        ),
        array(
            'name' => __('Dark blue', 'your-textdomain'),
            'slug' => 'dark-blue',
            'color' => '#1d2735',
        ),
        array(
            'name' => __('Blue', 'your-textdomain'),
            'slug' => 'blue',
            'color' => '#00659b',
        ),
        array(
            'name' => __('Light blue', 'your-textdomain'),
            'slug' => 'light-blue',
            'color' => '#4999ca',
        ),
    ));
 
}
 
/**
 * Hook: after_setup_theme.
 *
 * @uses add_action() https://developer.wordpress.org/reference/functions/add_action/
 * @uses after_setup_theme https://developer.wordpress.org/reference/hooks/after_setup_theme/
 */
add_action( 'after_setup_theme' , 'change_gutenberg_color_palette' );

使用上面的代碼,我們將一個(gè)操作添加到after_setup_theme鉤子中,并注冊(cè)一個(gè)名為change_gutenberg_color_palette的回調(diào)函數(shù)。

change_gutenberg_color_palette函數(shù)中,我們使用add_theme_support函數(shù)來(lái)啟用editor-color-palette主題支持。然后在第二個(gè)參數(shù),傳遞一個(gè)包含定義自定義顏色的數(shù)組的數(shù)組。

每個(gè)子數(shù)組包含三個(gè)鍵/值對(duì)。即:

  • $name: 我們要在編輯器中顯示的名稱(chēng)。請(qǐng)注意,我們使用__()函數(shù)使這些名稱(chēng)可翻譯。
  • $slug: 一個(gè)唯一的 slug 別名,我們可以在CSS中使用它來(lái)更改實(shí)際顏色。
  • $color: 十六進(jìn)制顏色值。

添加自定義CSS樣式代碼

為了使顏色真正在我們的主題中起作用,我們必須為每種顏色添加一些CSS,如下所示:

// Blackish
.has-blackish-background-color {
    background-color: #323232;
}
 
.has-blackish-color {
    color: #323232;
}

// Whiteish
.has-whiteish-background-color {
    background-color: #eeeeee;
}
 
.has-whiteish-color {
    color: #eeeeee;
}

// 等等...

禁用自定義顏色選擇器

上面的代碼可以使我們的用戶(hù)能夠使用自定義顏色選擇器制作自己的顏色。如果你希望用戶(hù)只能從上面的默認(rèn)顏色中選擇,不可以自定義顏色,可以通過(guò)下面的代碼實(shí)現(xiàn):

<?php
/**
 * disable_custom_color_picler.
 *
 * Disable the custom color selector of the gutenberg color palette,
 *
 * @see https://since1979.dev/wp-snippet-002-changing-the-gutenberg-color-palette/
 *
 * @uses add_theme_support https://developer.wordpress.org/reference/functions/add_theme_support/
 */
function disable_custom_color_picker()
{
    add_theme_support('disable-custom-colors');
}
/**
 * Hook: after_setup_theme.
 *
 * @uses add_action() https://developer.wordpress.org/reference/functions/add_action/
 * @uses after_setup_theme https://developer.wordpress.org/reference/hooks/after_setup_theme/
 */
add_action('after_setup_theme', 'disable_custom_color_picker');

使用上面的代碼,我們向after_setup_theme鉤子添加了另一個(gè)操作,并注冊(cè)了一個(gè)名為disable_custom_color_picker的回調(diào)函數(shù)。

disable_custom_color_picker函數(shù)內(nèi)部,我們?cè)俅问褂?code>add_theme_support函數(shù),但這一次我們添加了對(duì)disable-custom-colors的支持

如下圖所示,這會(huì)從面板上刪除“自定義顏色”鏈接。

內(nèi)容參考:https://since1979.dev/wp-snippet-002-changing-the-gutenberg-color-palette/

聲明:本站所有文章,如無(wú)特殊說(shuō)明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個(gè)人或組織,在未征得本站同意時(shí),禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書(shū)籍等各類(lèi)媒體平臺(tái)。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號(hào) WPDAXUE
WordPress開(kāi)發(fā)

WordPress自定義古騰堡編輯器的漸變色

2020-4-19 9:31:54

WordPress開(kāi)發(fā)

WordPress添加自定義字段到菜單項(xiàng)

2020-4-20 18:32:28

0 條回復(fù) A文章作者 M管理員
    暫無(wú)討論,說(shuō)說(shuō)你的看法吧
?
個(gè)人中心
購(gòu)物車(chē)
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

建阳市| 卢龙县| 金湖县| 宣城市| 洛宁县| 东至县| 东莞市| 新泰市| 隆林| 茶陵县| 罗源县| 洪湖市| 麦盖提县| 百色市| 铜陵市| 茂名市| 桓仁| 南安市| 汉川市| 南宁市| 德阳市| 河曲县| 崇仁县| 隆安县| 贵港市| 内江市| 松潘县| 永善县| 镇原县| 唐山市| 翼城县| 酉阳| 灵丘县| 高安市| 鹤峰县| 山丹县| 土默特左旗| 清丰县| 乐平市| 富民县| 东方市|