禁用古登堡文本-所有块中的设置

时间:2019-01-24 作者:public9nf

我需要在所有块中禁用古腾堡文本设置选项卡。功能中的功能是否可能。php?

我可以禁用“颜色”选项卡,但找不到文本设置的解决方案:

function disable_tabs() {
    add_theme_support( \'editor-color-palette\' );
    add_theme_support( \'disable-custom-colors\' );
}
add_action( \'after_setup_theme\', \'disable_tabs\' );

The text-settings tab

4 个回复
SO网友:WebElaine

我能找到的最近的the documentation 正在禁用自定义字体大小(文本输入),并强制下拉字体大小仅包含“正常”。

add_action(\'after_setup_theme\', \'wpse_remove_custom_colors\');
function wpse_remove_custom_colors() {
    // removes the text box where users can enter custom pixel sizes
    add_theme_support(\'disable-custom-font-sizes\');
    // forces the dropdown for font sizes to only contain "normal"
    add_theme_support(\'editor-font-sizes\', array(
        array(
            \'name\' => \'Normal\',
            \'size\' => 16,
            \'slug\' => \'normal\'
        )
    ) );
}
注意,这可能不适用于非核心块-它们可能有自己的注册字体大小等的方式,而不受theme\\u支持的影响。希望社区中的其他人也知道如何禁用下降上限。

Update: a way to remove Drop Caps

这不是我的首选方法,因为您仍然可以在编辑器中添加一个下拉帽,它只是不在前端渲染,但这是我迄今为止所能实现的全部:

有一个render_block() 钩子,您可以更改前端的渲染内容,而无需更改编辑器中显示的内容或保存到数据库中的内容。

add_filter(\'render_block\', function($block_content, $block) {
    // only affect the Core Paragraph block
    if(\'core/paragraph\' === $block[\'blockName\']) {
        // remove the class that creates the drop cap
        $block_content = str_replace(\'class="has-drop-cap"\', \'\', $block_content);
    }
    // always return the content, whether we changed it or not
    return $block_content;
}, 10, 2);
此方法会更改外观而不是实际内容,因此,如果您希望允许使用首字下沉,只需删除过滤器,就会突然出现人们在编辑器中添加的所有过滤器。

SO网友:fairport

There\'s good answers about disabling the font styles but not about disabling the drop cap.

In WordPress 5.8, you can you the new theme.json feature to disable drop caps in your theme. You need to add a file with name theme.json in the root of your theme. It should have the following content:

{
    "version": 1,
    "settings": {
        "typography": {
            "dropCap": false
        }
    }
}

If you want to use a filter instead, you can use the following code in WordPress 5.8:

function disable_drop_cap_ editor_settings_5_8(array $editor_settings): array {
    $editor_settings[\'__experimentalFeatures\'][\'typography\'][\'dropCap\'] = false;
    return $editor_settings;
}
add_filter(\'block_editor_settings\', \'disable_drop_cap_ editor_settings_5_8\');

In WordPress 5.7, the drop cap can be disabled with the following code:

function disable_drop_cap_editor_settings_5_7(array $editor_settings): array {
    $editor_settings[\'__experimentalFeatures\'][\'defaults\'][\'typography\'][\'dropCap\'] = false;
    return $editor_settings;
}
add_filter(\'block_editor_settings\', \'disable_drop_cap_editor_settings_5_7\');

In WordPress 5.6, the following code works:

function disable_drop_cap_editor_settings_5_6(array $editor_settings): array {
    $editor_settings[\'__experimentalFeatures\'][\'global\'][\'typography\'][\'dropCap\'] = false;
    return $editor_settings;
}
add_filter(\'block_editor_settings\', \'disable_drop_cap_editor_settings_5_6\');

In WordPress 5.5, you will have to use JavaScript to accomplish the same thing:

function disable_drop_cap_admin_footer() {
    echo <<<HTML
<script>
document.addEventListener("DOMContentLoaded", function () {
  var removeDropCap = function(settings, name) {
      
    if (name !== "core/paragraph") {
      return settings;
    }
    var newSettings = Object.assign({}, settings);
    if (
      newSettings.supports &&
      newSettings.supports.__experimentalFeatures &&
      newSettings.supports.__experimentalFeatures.typography &&
      newSettings.supports.__experimentalFeatures.typography.dropCap
    ) {
      newSettings.supports.__experimentalFeatures.typography.dropCap = false;
    }
    return newSettings;
  };
  wp.hooks.addFilter(
    "blocks.registerBlockType",
    "sc/gb/remove-drop-cap",
    removeDropCap,
  );
});
</script>
HTML;
}
add_action(\'admin_footer\', \'disable_drop_cap_admin_footer\');

If you want the functionality as a plugin, you can use the Disable Drop Cap plugin. Full disclosure, I\'m the author of the said plugin.

SO网友:jg314

Jim Rhoades 在其中一条注释中指出,您可以声明支持自定义字体大小,但不包括字体大小选项数组。这将删除;“排版”;所以没有办法选择字体大小。代码如下所示:

add_theme_support( \'editor-font-sizes\' );
对于那些寻找使其工作所需的所有代码的人,您可以从after_setup_theme 钩住函数内部。主题中的php文件:

function theme_setup() {
    add_theme_support( \'editor-font-sizes\' );
}
add_action( \'after_setup_theme\', \'theme_setup\' );
我已经在WordPress 5.7中对此进行了测试,但在这个版本之前,它可能是受支持的。

SO网友:Kevin Leary

这是真实的、最终的答案,解决了原始问题中描述的确切情况:

/**
 * Disable Native Gutenberg Features
 */
function gutenberg_removals()
{
  add_theme_support(\'disable-custom-font-sizes\');
  add_theme_support(\'editor-font-sizes\', []);
}
add_action(\'after_setup_theme\', \'gutenberg_removals\');
将空数组传递给editor-font-sizes 避免PHP通知无效参数。

相关推荐