Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Last active August 25, 2018 11:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GaryJones/1960177 to your computer and use it in GitHub Desktop.
Save GaryJones/1960177 to your computer and use it in GitHub Desktop.
Enqueue preferred and alternative style sheets.
<?php
add_action( 'wp_enqueue_scripts', 'child_add_alternate_style_sheet' );
/**
* Enqueue preferred and alternate style sheet.
*
* WordPress already adds a persistent style sheet (style.css), so this
* function gives examples of adding a preferred and alternative style sheets.
*
* Persistent = browser MUST apply it.
* Preferred = browser SHOULD apply it, unless user has selected a different alternative.
* Alternative = browser should allow users to select from these mutually exclusive style sheets.
*
* @author Gary Jones
* @link https://garyjones.io/enqueued-style-sheet-extras
* @see https://www.w3.org/TR/html4/present/styles.html#h-14.3.1
*/
function child_add_alternate_style_sheet() {
global $wp_styles;
// Add a prefered style sheet, for the normal contrast (colours, font sizes etc)
wp_enqueue_style( 'child-default', get_stylesheet_directory_uri() . '/style-default.css', [], '1.0' );
$wp_styles->add_data( 'child-default', 'title', __( 'Default', 'child-theme-textdomain' ) );
// Add an alternative style sheet, high contrast.
wp_enqueue_style( 'child-highcontrast', get_stylesheet_directory_uri() . '/style-highcontrast.css', [], '1.0' );
$wp_styles->add_data( 'child-highcontrast', 'title', __( 'High Contrast', 'child-theme-textdomain' ) );
$wp_styles->add_data( 'child-highcontrast', 'alt', true );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment