Skip to content

Instantly share code, notes, and snippets.

@seothemes
Created February 5, 2017 08:27
Show Gist options
  • Save seothemes/d053dc092344613dc3b4b1b47f0ac463 to your computer and use it in GitHub Desktop.
Save seothemes/d053dc092344613dc3b4b1b47f0ac463 to your computer and use it in GitHub Desktop.
<?php
/**
* Requires the WP-SCSS plugin to be installed and activated.
*
* @package Customizer Sass
* @link https://seothemes.net
* @author Seo Themes
* @copyright Copyright © 2017 Seo Themes
* @license GPL-2.0+
*/
// Check if plugin is active
if ( !is_plugin_active( 'wp-scss/wp-scss.php' ) ) {
return;
}
// Always recompile in the customizer
if ( is_customize_preview() && !defined('WP_SCSS_ALWAYS_RECOMPILE')) {
define('WP_SCSS_ALWAYS_RECOMPILE', true);
}
// Update the default paths to match theme
$seo_wpscss_options = get_option('wpscss_options');
if( $seo_wpscss_options['scss_dir'] !== '/sass/' || $seo_wpscss_options['css_dir'] !== '/' ) {
// Alter the options array appropriately
$seo_wpscss_options['scss_dir'] = '/sass/';
$seo_wpscss_options['css_dir'] = '/';
// Update entire array
update_option('wpscss_options', $seo_wpscss_options);
}
// Create array of default colors
$seo_default_colors = array(
'color-text' => '#333333',
'color-button' => '#333333',
'color-link' => '#dddddd',
'color-white' => '#ffffff',
'color-outline' => '#eeeeee',
);
// Update SCSS variables
function wp_scss_set_variables(){
// Get the default colors
global $seo_default_colors;
// Prepare the array
$variables = array();
// Loop through each variable and get theme_mod
foreach( $seo_default_colors as $key => $value ) {
$variables[$key] = get_theme_mod( $key, $value );
}
return $variables;
}
add_filter('wp_scss_variables', 'wp_scss_set_variables');
// Register settings and controls with the Customizer.
add_action( 'customize_register', 'starter_customizer_register' );
function starter_customizer_register() {
global $wp_customize;
global $seo_default_colors;
// Loop through each variable in the array
foreach($seo_default_colors as $key => $value) {
// Add setting for each variable
$wp_customize->add_setting( $key , array(
'default' => $value,
'sanitize_callback' => 'sanitize_hex_color',
) );
// Add control for each variable
$wp_customize->add_control(
new WP_Customize_Color_Control(
$wp_customize,
$key,
array(
'label' => ucwords( str_replace( 'color-', '', $key ) ) . ' Color',
'section' => 'colors',
'settings' => $key,
) )
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment