Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mathetos/cbc2302477b32a95e70c227d75af0229 to your computer and use it in GitHub Desktop.
Save mathetos/cbc2302477b32a95e70c227d75af0229 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