Override TinyMCE's default colors. Created specifically for Classic Editor Block or ACF WYSIWYG in WordPress.

06/07/2024
Back to Gists
<?php
/**
 * Limit MCE WYSIWYG Colors
 *
 * This function overrides TinyMCE's default color options in the WordPress Classic Editor Block
 * or ACF (Advanced Custom Fields) WYSIWYG editor. It limits the available color choices.
 *
 * Gist Keywords: wordpress, editor, admin, tinymce, colors
 * Author: Knol Aust
 * Version: 1.0.0
 *
 * @param array $init The TinyMCE editor initialization settings.
 * @return array The modified TinyMCE editor initialization settings.
 */
function knolaust_override_MCE_options($init)
{
    // Define custom colors with their corresponding HEX codes and names
    $custom_colors = '
        "fabb2b", "Yellow",
        "e5ebf0", "Light Blue",
        "003b71", "Medium Blue",
        "042240", "Dark Blue",
        "f16061", "Red",
        "929292", "Medium Gray",
        "262626", "Charcoal"
    ';

    // Build the color grid palette using custom colors
    $init['textcolor_map'] = '[' . $custom_colors . ']';

    // Change the number of rows in the color grid (1 row in this case)
    $init['textcolor_rows'] = 1;

    return $init;
}

// Add a filter to apply the custom color settings
add_filter('tiny_mce_before_init', 'knolaust_override_MCE_options');
?>