【问题标题】:How to add custom TinyMCE button to ACF editor?如何将自定义 TinyMCE 按钮添加到 ACF 编辑器?
【发布时间】:2021-05-02 21:48:24
【问题描述】:

我已将自定义 tinyMCE 按钮添加到编辑器,但我不知道如何将其添加到 ACF 中的编辑器。

function mce_cta_button() {
    if ( is_admin() ) {
        add_filter( 'mce_external_plugins', 'custom_tinymce_plugin' );
        add_filter( 'mce_buttons', 'register_mce_buttons' );
    }
}
add_action('admin_head', 'mce_cta_button');

我已经尝试添加这个:

add_filter( 'acf/fields/wysiwyg/toolbars' , 'my_toolbars'  );
function my_toolbars( $toolbars )
{
    return array();
}

然后它工作,但我得到错误:

Warning: array_unshift() expects parameter 1 to be array, null given in .../themes/behold-standard/libs/others.php on line 50

Warning: implode(): Invalid arguments passed in .../plugins/advanced-custom-fields-pro/includes/fields/class-acf-field-wysiwyg.php on line 176

在 others.php 文件中我有这个脚本:

function customToolbars( $toolbars ) {
    array_unshift( $toolbars ['Basic'][1], 'fontsizeselect', 'forecolor', 'subscript', 'superscript' );
    return $toolbars;
}
add_filter( 'acf/fields/wysiwyg/toolbars' , 'customToolbars'  );

我的错误在哪里?

【问题讨论】:

    标签: tinymce advanced-custom-fields


    【解决方案1】:

    TL/DR

    您根本不需要my_toolbars() 代码块。单独使用customToolbars() 将产生您正在寻找的结果。请注意,ACF 中有多种工具栏配置,包括基本和完整。根据您的自定义字段使用的配置,您可能需要将自定义添加到两者。

    加长版

    此代码正在清空 ACF 中的 TinyMCE 工具栏配置:

    add_filter( 'acf/fields/wysiwyg/toolbars' , 'my_toolbars'  );
    function my_toolbars( $toolbars )
    {
        return array();
    }
    

    由于上面的代码,当您的customToolbars() 运行时,$toolbars 是一个空数组,这意味着$toolbars['Basic'][1] 不存在,而array_unshift 实际上是通过null 而不是一个数组。

    function customToolbars( $toolbars ) {
        array_unshift( $toolbars ['Basic'][1], 'fontsizeselect', 'forecolor', 'subscript', 'superscript' );
        return $toolbars;
    }
    add_filter( 'acf/fields/wysiwyg/toolbars' , 'customToolbars'  );
    

    最终,这段代码自己会做你想做的事:

    function customToolbars( $toolbars ) {
        // add to basic toolbar configuration
        array_unshift( $toolbars ['Basic'][1], 'fontsizeselect', 'forecolor', 'subscript', 'superscript' );
    
        // add to full toolbar configuration
        array_unshift( $toolbars ['Full'][1], 'fontsizeselect', 'forecolor', 'subscript', 'superscript' );
        return $toolbars;
    }
    add_filter( 'acf/fields/wysiwyg/toolbars' , 'customToolbars'  );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多