Using WordPress ‘mce_buttons’ PHP filter

The mce_buttons WordPress PHP filter allows you to modify the first-row list of TinyMCE buttons in the Visual tab.

Usage

add_filter('mce_buttons', 'your_function_name', 10, 2);
function your_function_name($mce_buttons, $editor_id) {
    // your custom code here
    return $mce_buttons;
}

Parameters

  • $mce_buttons (array) – The first-row list of buttons.
  • $editor_id (string) – Unique editor identifier, e.g. ‘content’. Accepts ‘classic-block’ when called from block editor’s Classic block.

More information

See WordPress Developer Resources: mce_buttons

Examples

Add a custom button to the first row

Add a custom button called ‘your_button’ to the first row of TinyMCE buttons in the Visual tab.

add_filter('mce_buttons', 'add_custom_button', 10, 2);
function add_custom_button($mce_buttons, $editor_id) {
    array_push($mce_buttons, 'your_button');
    return $mce_buttons;
}

Remove the bold button

Remove the bold button from the first row of TinyMCE buttons in the Visual tab.

add_filter('mce_buttons', 'remove_bold_button', 10, 2);
function remove_bold_button($mce_buttons, $editor_id) {
    $remove_button = 'bold';
    $index = array_search($remove_button, $mce_buttons);
    if (false !== $index) {
        unset($mce_buttons[$index]);
    }
    return $mce_buttons;
}

Move the strikethrough button to the first row

Move the strikethrough button from the second row to the first row of TinyMCE buttons in the Visual tab.

add_filter('mce_buttons', 'move_strikethrough_button', 10, 2);
function move_strikethrough_button($mce_buttons, $editor_id) {
    array_push($mce_buttons, 'strikethrough');
    return $mce_buttons;
}

Rearrange buttons in the first row

Rearrange the buttons in the first row of TinyMCE buttons in the Visual tab.

add_filter('mce_buttons', 'rearrange_buttons', 10, 2);
function rearrange_buttons($mce_buttons, $editor_id) {
    $new_order = array(
        'bold', 'italic', 'underline', 'strikethrough',
        'alignleft', 'aligncenter', 'alignright', 'alignjustify',
    );
    return $new_order;
}

Change buttons for the Classic block

Change the first-row buttons for the Classic block in the block editor.

add_filter('mce_buttons', 'change_classic_block_buttons', 10, 2);
function change_classic_block_buttons($mce_buttons, $editor_id) {
    if ('classic-block' === $editor_id) {
        $new_buttons = array(
            'bold', 'italic', 'underline',
            'alignleft', 'aligncenter', 'alignright',
        );
        return $new_buttons;
    }
    return $mce_buttons;
}