Dynamically Change Styles Based on Query Parameters

Last updated 1 month ago

PHP
function load_styles_based_on_url() {
    if ( isset( $_GET['theme_mode'] ) && $_GET['theme_mode'] == 'dark' ) {
        wp_enqueue_style( 'dark-theme', get_template_directory_uri() . '/css/dark-theme.css' );
    } elseif ( isset( $_GET['theme_mode'] ) && $_GET['theme_mode'] == 'light' ) {
        wp_enqueue_style( 'light-theme', get_template_directory_uri() . '/css/light-theme.css' );
    }
}
add_action( 'wp_enqueue_scripts', 'load_styles_based_on_url' );

All code snippets are licensed GPLv2 or later unless otherwise stated.