当前位置:首页>WordPress建站>WordPress开发>WordPress自定义古腾堡编辑器的颜色调色板

WordPress自定义古腾堡编辑器的颜色调色板

古腾堡Gutenberg编辑器可以为段落等区块设置文本颜色和背景色等,而且调色板中提供了12种默认颜色:

但是这些颜色可能不一定适配我们的主题,虽然也支持自定义设置,但是如果我们将这些默认颜色选项统一修改了,用户只需直接点击即可应用,对用户来说那就极为便利了。

自定义古腾堡颜色调色板

要实现上图右边的自定义配色方案,可以使用下面的代码:

  1. <?php
  2. /**
  3. * change_gutenberg_color_palette.
  4. *
  5. * Add theme support for editor-color-palette,
  6. * and set colours for the gutenberg colour pallete.
  7. *
  8. * @see https://since1979.dev/wp-snippet-002-changing-the-gutenberg-color-palette/
  9. *
  10. * @uses add_theme_support() https://developer.wordpress.org/reference/functions/add_theme_support/
  11. * @uses __() https://developer.wordpress.org/reference/functions/__/
  12. * @uses array() https://www.php.net/manual/en/function.array.php
  13. *
  14. * @return void
  15. */
  16. function change_gutenberg_color_palette() {
  17. add_theme_support( 'editor-color-palette', array(
  18. array(
  19. 'name' => __('Blackish', 'your-textdomain'),
  20. 'slug' => 'blackish',
  21. 'color' => '#323232',
  22. ),
  23. array(
  24. 'name' => __('Whiteish', 'your-textdomain'),
  25. 'slug' => 'white',
  26. 'color' => '#eeeeee',
  27. ),
  28. array(
  29. 'name' => __('White', 'your-textdomain'),
  30. 'slug' => 'white',
  31. 'color' => '#ffffff',
  32. ),
  33. array(
  34. 'name' => __('Dark blue', 'your-textdomain'),
  35. 'slug' => 'dark-blue',
  36. 'color' => '#1d2735',
  37. ),
  38. array(
  39. 'name' => __('Blue', 'your-textdomain'),
  40. 'slug' => 'blue',
  41. 'color' => '#00659b',
  42. ),
  43. array(
  44. 'name' => __('Light blue', 'your-textdomain'),
  45. 'slug' => 'light-blue',
  46. 'color' => '#4999ca',
  47. ),
  48. ));
  49. }
  50. /**
  51. * Hook: after_setup_theme.
  52. *
  53. * @uses add_action() https://developer.wordpress.org/reference/functions/add_action/
  54. * @uses after_setup_theme https://developer.wordpress.org/reference/hooks/after_setup_theme/
  55. */
  56. add_action( 'after_setup_theme' , 'change_gutenberg_color_palette' );

使用上面的代码,我们将一个操作添加到after_setup_theme钩子中,并注册一个名为change_gutenberg_color_palette的回调函数。

change_gutenberg_color_palette函数中,我们使用add_theme_support函数来启用editor-color-palette主题支持。然后在第二个参数,传递一个包含定义自定义颜色的数组的数组。

每个子数组包含三个键/值对。即:

  • $name: 我们要在编辑器中显示的名称。请注意,我们使用__()函数使这些名称可翻译。
  • $slug: 一个唯一的 slug 别名,我们可以在CSS中使用它来更改实际颜色。
  • $color: 十六进制颜色值。

添加自定义CSS样式代码

为了使颜色真正在我们的主题中起作用,我们必须为每种颜色添加一些CSS,如下所示:

  1. // Blackish
  2. .has-blackish-background-color {
  3. background-color: #323232;
  4. }
  5. .has-blackish-color {
  6. color: #323232;
  7. }
  8. // Whiteish
  9. .has-whiteish-background-color {
  10. background-color: #eeeeee;
  11. }
  12. .has-whiteish-color {
  13. color: #eeeeee;
  14. }
  15. // 等等...

禁用自定义颜色选择器

上面的代码可以使我们的用户能够使用自定义颜色选择器制作自己的颜色。如果你希望用户只能从上面的默认颜色中选择,不可以自定义颜色,可以通过下面的代码实现:

  1. <?php
  2. /**
  3. * disable_custom_color_picler.
  4. *
  5. * Disable the custom color selector of the gutenberg color palette,
  6. *
  7. * @see https://since1979.dev/wp-snippet-002-changing-the-gutenberg-color-palette/
  8. *
  9. * @uses add_theme_support https://developer.wordpress.org/reference/functions/add_theme_support/
  10. */
  11. function disable_custom_color_picker()
  12. {
  13. add_theme_support('disable-custom-colors');
  14. }
  15. /**
  16. * Hook: after_setup_theme.
  17. *
  18. * @uses add_action() https://developer.wordpress.org/reference/functions/add_action/
  19. * @uses after_setup_theme https://developer.wordpress.org/reference/hooks/after_setup_theme/
  20. */
  21. add_action('after_setup_theme', 'disable_custom_color_picker');

使用上面的代码,我们向after_setup_theme钩子添加了另一个操作,并注册了一个名为disable_custom_color_picker的回调函数。

disable_custom_color_picker函数内部,我们再次使用add_theme_support函数,但这一次我们添加了对disable-custom-colors的支持

如下图所示,这会从面板上删除“自定义颜色”链接。

内容参考:https://since1979.dev/wp-snippet-002-changing-the-gutenberg-color-palette/

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

WordPress自定义古腾堡编辑器的渐变色

2020-4-19 9:31:54

WordPress开发

WordPress添加自定义字段到菜单项

2020-4-20 18:32:28

0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
私信列表
搜索