Files
chrnx-dev-wp/inc/theme-preference.php

19 lines
979 B
PHP

<?php
/**
* Early, local-only theme preference bootstrap.
*
* @package CHRNX
*/
/** Print the no-flash theme resolution before stylesheet links. */
function chrnx_print_theme_preference_bootstrap() {
$script = "(function(){var t=null,m;try{var s=localStorage.getItem('chrnx-theme');if(s==='light'||s==='dark'){t=s;}}catch(e){}if(!t){try{m=matchMedia('(prefers-color-scheme: dark)').matches;t=m?'dark':'light';}catch(e){t='dark';}}document.documentElement.dataset.theme=t;document.documentElement.style.colorScheme=t;document.documentElement.classList.add('js','chrnx-theme-enhanced');}());";
// wp_get_inline_script_tag applies WordPress's standard CSP attribute filter.
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Core escapes tag attributes and script is a fixed literal.
echo wp_get_inline_script_tag(
$script,
array( 'id' => 'chrnx-theme-preference-bootstrap' )
);
}
add_action( 'wp_head', 'chrnx_print_theme_preference_bootstrap', 0 );