Created
March 21, 2025 18:52
-
-
Save KittenCodes/16dbc6e6670a7fa433db9a8d3450a20d to your computer and use it in GitHub Desktop.
Oxygen 6 - Dynamic Colours via ACF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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