Using WordPress ‘mce_buttons_2’ PHP filter

The mce_buttons_2 WordPress PHP filter allows you to modify the second-row list of TinyMCE buttons in the Visual tab of the WordPress editor.

Usage

add_filter( 'mce_buttons_2', 'your_custom_function', 10, 2 );

function your_custom_function( $mce_buttons_2, $editor_id ) {
    // your custom code here
    return $mce_buttons_2;
}

Parameters

  • $mce_buttons_2 (array): Second-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_2

Examples

Add a button to the second row

Add a custom button to the second row of TinyMCE buttons:

add_filter( 'mce_buttons_2', 'add_custom_button', 10, 2 );

function add_custom_button( $mce_buttons_2, $editor_id ) {
    $mce_buttons_2[] = 'custom_button';
    return $mce_buttons_2;
}

Remove a button from the second row

Remove the ‘strikethrough’ button from the second row of TinyMCE buttons:

add_filter( 'mce_buttons_2', 'remove_strikethrough_button', 10, 2 );

function remove_strikethrough_button( $mce_buttons_2, $editor_id ) {
    $key = array_search( 'strikethrough', $mce_buttons_2 );
    if ( false !== $key ) {
        unset( $mce_buttons_2[ $key ] );
    }
    return $mce_buttons_2;
}

Rearrange buttons in the second row

Rearrange the buttons in the second row of TinyMCE buttons:

add_filter( 'mce_buttons_2', 'rearrange_buttons', 10, 2 );

function rearrange_buttons( $mce_buttons_2, $editor_id ) {
    // Define the new order of buttons
    $new_order = array( 'bold', 'italic', 'underline', 'strikethrough' );

    // Reorder the buttons according to the new order
    $mce_buttons_2 = array_intersect( $new_order, $mce_buttons_2 );

    return $mce_buttons_2;
}

Add a separator between buttons

Add a separator between two buttons in the second row of TinyMCE buttons:

add_filter( 'mce_buttons_2', 'add_separator', 10, 2 );

function add_separator( $mce_buttons_2, $editor_id ) {
    $position = array_search( 'italic', $mce_buttons_2 );
    if ( false !== $position ) {
        array_splice( $mce_buttons_2, $position + 1, 0, '|' );
    }
    return $mce_buttons_2;
}

Add a custom button only for the Classic Block

Add a custom button to the second row of TinyMCE buttons only when the editor is the Classic Block:

add_filter( 'mce_buttons_2', 'add_custom_button_classic_block', 10, 2 );

function add_custom_button_classic_block( $mce_buttons_2, $editor_id ) {
    if ( 'classic-block' === $editor_id ) {
$mce_buttons_2[] = 'custom_button_classic_block';
}
return $mce_buttons_2;
}