Support

Account

Home Forums General Issues Color Picker Palette(s) more than one Reply To: Color Picker Palette(s) more than one

  • For anyone where $field.data('key') is invalid, the following code worked for me.

    The main difference is that you’re targeting $field[0]['dataset']['key'].

    <?php
    // Set the default color palette for certain fields
    function set_acf_color_picker_default_palettes() {
    ?>
    <script>
    let setDefaultPalette = function() {
        acf.add_filter('color_picker_args', function( args, $field ){
    
            // Find the field key
            let targetFieldKey = $field[0]['dataset']['key'];
    
            // Set color options for the field
            // if field is accordion_icon_background_color
            if ( 'field_60d621b2f0e04' === targetFieldKey ) {
                args.palettes = [ '#999', '#ae241a', '#004990' ];
            }
    
            // Return
            return args;
        });
    }
    setDefaultPalette();
    </script>
    <?php
    }
    add_action('acf/input/admin_footer', 'set_acf_color_picker_default_palettes');