Skip to content

Instantly share code, notes, and snippets.

@KittenCodes
Created March 21, 2025 18:52
Show Gist options
  • Save KittenCodes/16dbc6e6670a7fa433db9a8d3450a20d to your computer and use it in GitHub Desktop.
Save KittenCodes/16dbc6e6670a7fa433db9a8d3450a20d to your computer and use it in GitHub Desktop.
Oxygen 6 - Dynamic Colours via ACF
<?php
// Add a dynamic CSS variable to the <head> of your site
add_action('wp_head', function() {
if (function_exists('get_field')) {
// Retrieve the value of the ACF field
$selected_colour = get_field('select_colour');
// Check if a value is set and split the value and label if necessary
if ($selected_colour) {
$colour_value = explode(' : ', $selected_colour)[0]; // Extracts the hex value before " : "
} else {
$colour_value = '#0000ff'; // Default colour if not set
}
// Output the CSS variable to the site head
echo '<style>
:root {
--dynamic-colour: ' . esc_attr($colour_value) . ';
}
</style>';
}
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment