Skip to content

Instantly share code, notes, and snippets.

@kevinwhoffman
Last active July 14, 2016 06:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kevinwhoffman/fe597d31f77dafb21dc434b305d09150 to your computer and use it in GitHub Desktop.
Save kevinwhoffman/fe597d31f77dafb21dc434b305d09150 to your computer and use it in GitHub Desktop.
Build editor stylesheet from customizer settings
<?php
// Place in functions.php of Twenty Sixteen to see editor background take on color of customizer background.
/**
* Build editor stylesheet from customizer settings upon customizer save.
*/
function kwh_build_stylesheet() {
$upload_dir = wp_upload_dir();
$stylesheet = $upload_dir['basedir'] . '/kwh-editor-style.css';
$styles = '';
// Get customizer settings to be written to stylesheet.
$background_color = get_theme_mod( 'background_color' );
// Construct styles.
// Note "\n" must be in double quotes in order to be escaped.
$styles .= 'body { background-color: #' . $background_color . '; }' . "\n";
// Write styles to file.
file_put_contents( $stylesheet, $styles );
}
add_action( 'customize_save_after', 'kwh_build_stylesheet' );
/**
* Add editor stylesheet so customizer styles appear in editor.
*/
function kwh_add_editor_style() {
$upload_dir = wp_upload_dir();
$stylesheet_url = $upload_dir['baseurl'] . '/kwh-editor-style.css';
add_editor_style( $stylesheet_url );
}
add_action( 'admin_init', 'kwh_add_editor_style' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment