Customize the WYSIWYG toolbars

Last updated May 16, 2014

Description

The WYSIWYG field contains an option to define the toolbar (buttons) which are rendered onto the tinyMCE object.

By default, you can select ‘Full’ or ‘Basic’, however, it is possible to extend these toolbars to create your own, edit existing and even remove the existing choices.

Requirements

  • ACF version 3.5.8 or above

The Filter

The filter to modify the toolbars is: acf/fields/wysiwyg/toolbars

The WYSIWYG field uses an array (which can be modified as shown below) to hold a list of toolbars. Each toolbar comprises of a name and up to 4 rows of buttons. With this filter, you can add, edit and remove toolbars. This filter allows you to modify this very array.

Once a new toolbar is added to the list, it will then appear in the WYSIWYG field’s options when editing the field group. Once selected, the field will use the available rows to output the desired buttons

Usage

<?php

add_filter( 'acf/fields/wysiwyg/toolbars' , 'my_toolbars'  );
function my_toolbars( $toolbars )
{
    // Uncomment to view format of $toolbars
    /*
    echo '< pre >';
        print_r($toolbars);
    echo '< /pre >';
    die;
    */

    // Add a new toolbar called "Very Simple"
    // - this toolbar has only 1 row of buttons
    $toolbars['Very Simple' ] = array();
    $toolbars['Very Simple' ][1] = array('bold' , 'italic' , 'underline' );

    // Edit the "Full" toolbar and remove 'code'
    // - delet from array code from http://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key
    if( ($key = array_search('code' , $toolbars['Full' ][2])) !== false )
    {
        unset( $toolbars['Full' ][2][$key] );
    }

    // remove the 'Basic' toolbar completely
    unset( $toolbars['Basic' ] );

    // return $toolbars - IMPORTANT!
    return $toolbars;
}

?>
  • Use this filter in your functions.php file

Related