Skip to content

Instantly share code, notes, and snippets.

@jonschr
Created March 22, 2022 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonschr/02a320eb5d8ed5e5917e28fea0c9b1cc to your computer and use it in GitHub Desktop.
Save jonschr/02a320eb5d8ed5e5917e28fea0c9b1cc to your computer and use it in GitHub Desktop.
<?php
function elodin_section_block_get_the_colors_formatted_for_acf_from_theme_support() {
// get the colors
$color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
// bail if there aren't any colors found
if ( !$color_palette )
return;
// output begins
ob_start();
// output the names in a string for use in Iris
echo '[';
foreach ( $color_palette as $color ) {
echo "'" . $color['color'] . "', ";
}
echo ']';
return ob_get_clean();
}
function elodin_section_block_get_the_colors_formatted_for_acf_from_theme_json() {
ob_start();
// get the palette from the theme defaults, as defined in theme.json
$color_palette = [];
if ( class_exists( 'WP_Theme_JSON_Resolver' ) ) {
$settings = WP_Theme_JSON_Resolver::get_theme_data()->get_settings();
if ( isset( $settings['color']['palette']['theme'] ) ) {
$color_palette = $settings['color']['palette']['theme'];
}
}
// output the names in a string for use in Iris
echo '[';
foreach ( $color_palette as $color ) {
echo "'" . $color['color'] . "', ";
}
echo ']';
return ob_get_clean();
}
add_action( 'acf/input/admin_footer', 'elodin_section_block_register_acf_color_palette' );
function elodin_section_block_register_acf_color_palette() {
$color_palette_json = elodin_section_block_get_the_colors_formatted_for_acf_from_theme_json();
$color_palette_theme_support = elodin_section_block_get_the_colors_formatted_for_acf_from_theme_support();
//* use theme.json values first, then fall back to add_editor_support colors if not. Bail if there aren't any.
if ( $color_palette_json !== null ) {
$color_palette = $color_palette_json;
} elseif ( $color_palette_theme_support !== null ) {
$color_palette = $color_palette_theme_support;
} else {
return;
}
?>
<script type="text/javascript">
(function( $ ) {
acf.add_filter( 'color_picker_args', function( args, $field ){
// add the hexadecimal codes here for the colors you want to appear as swatches
args.palettes = <?php echo $color_palette; ?>
// return colors
return args;
});
})(jQuery);
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment