Skip to content

Instantly share code, notes, and snippets.

@justintadlock
Last active September 20, 2022 19:27
Show Gist options
  • Save justintadlock/ff27b00de0b28726b3336cde8d01806b to your computer and use it in GitHub Desktop.
Save justintadlock/ff27b00de0b28726b3336cde8d01806b to your computer and use it in GitHub Desktop.
Converts piped "preset" colors from global styles to their var() equivalents.
<?php
// Converts piped "preset" colors from global styles to their var() equivalents.
// Borrowed from `wp_render_elements_support_styles()`
function jt_format_color_preset( $color ) {
if ( strpos( $color, 'var:preset|color|' ) !== false ) {
$index = strrpos( $color, '|' ) + 1;
$name = substr( $color, $index );
$color = "var(--wp--preset--color--$name)";
}
return esc_html( $color );
}
@justintadlock
Copy link
Author

justintadlock commented Sep 20, 2022

Usage:

background-color: <?= jt_format_color_preset( wp_get_global_styles( [ 'background', 'color' ] ) ) ?>;

// outputs:
// color: var( --wp--preset--color--{$name} );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment