D_P
D_P

Reputation: 107

How to add custom TinyMCE button to ACF editor?

I have added custom tinyMCE button to editor, but I can't figure out how to add it to editor in 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');

I have tried to add this:

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

then it works, but I get error:

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

in others.php file I have this script:

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

where is my mistake?

Upvotes: 0

Views: 2139

Answers (2)

wp-mario.ru
wp-mario.ru

Reputation: 914

Use the 'init' event instead of 'admin_head' and everything will work out.

Upvotes: 0

cavemon
cavemon

Reputation: 11

TL/DR

You don't need the my_toolbars() code block at all. Using the customToolbars() by itself will yield the results you are looking for. Just note that there are multiple toolbar configurations in ACF including Basic and Full. Depending on which configuration your custom field is using, you may need to add your customizations to both.

Long version

This code is emptying the TinyMCE toolbar configuration from ACF:

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

Because of the code above, when your customToolbars() runs, $toolbars is an empty array, which means $toolbars['Basic'][1] does not exist, and array_unshift is literally being passed null instead of an array.

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

Ultimately this code by itself will do what you want:

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'  );

Upvotes: 1

Related Questions