Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implement the Sass compiler in PHP
- * https://github.com/leafo/scssphp
- * This requires PHP 5.3 or higher because it uses namespacing.
- */
- require get_template_directory() . '/inc/scssphp/scss.inc.php';
- use Leafo\ScssPhp\Compiler;
- /* First, add the controls to the Customizer itself. */
- function sass_customizer( $wp_customize ) {
- $wp_customize->add_setting( 'color_scheme', array(
- 'default' => '#16dff9',
- 'transport' => 'postMessage'
- ) );
- $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'color_scheme', array(
- 'label' => 'Sass color picker',
- 'section' => 'colors'
- ) ) );
- }
- add_action( 'customize_register', 'sass_customizer' );
- /* This function runs after the customizer options are saved, generating the appropriate stylesheet. */
- function compile_sass() {
- $scss = new Compiler();
- $base_color = get_theme_mod( 'color_scheme' );
- $compiler_string = '$base-color: %1$s; a { color: $base-color; &:hover { color: darken( $base-color, 15 ); } };';
- $compiler_string = sprintf( $compiler_string, $base_color );
- $compiled = $scss->compile( $compiler_string );
- $file = fopen( get_template_directory() . '/css/customizer.css', 'a+' );
- if ( $file ) {
- fwrite( $file, $compiled );
- fclose( $file );
- }
- }
- add_action( 'customize_save_after', 'compile_sass' );
- /* Then, load the newly-created stylesheet. */
- function load_customizer_css() {
- wp_enqueue_style( 'customizer-css', get_template_directory_uri() . '/css/customizer.css' );
- }
- add_action( 'wp_enqueue_scripts', 'load_customizer_css' );
- /* Alternatively, write the CSS directly to the header. */
- function write_css_to_head() {
- $scss = new Compiler();
- $base_color = get_theme_mod( 'color_scheme' );
- $compiler_string = '$base-color: %1$s; a { color: $base-color; &:hover { color: darken( $base-color, 15 ); } };';
- $compiler_string = sprintf( $compiler_string, $base_color );
- $compiled = $scss->compile( $compiler_string );
- echo '<style type="text/css">' . $compiled . '</style>';
- }
- add_action( 'wp_head', 'write_css_to_head' );
- /* End implementation of the Sass compiler in PHP. This is only a test. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement