Skip to content

Instantly share code, notes, and snippets.

@mattyza
Created September 10, 2013 15:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mattyza/44ec00e89401d7023ae6 to your computer and use it in GitHub Desktop.
Save mattyza/44ec00e89401d7023ae6 to your computer and use it in GitHub Desktop.
Inject alternate styles from a child theme into your theme's theme options stylesheet switcher.
function woo_options_add ( $o ) {
// If our options aren't an array, get out.
if ( ! is_array( $o ) ) return $o;
$theme_dir = get_option( 'template' );
$style_dir = get_option( 'stylesheet' );
// Loop through until we get to the woo_alt_stylesheet option.
foreach ( $o as $k => $v ) {
if ( !isset( $v['id'] ) || 'woo_alt_stylesheet' != $v['id'] ) continue;
// Force a "select2" field type.
$o[$k]['type'] = 'select2';
// Transform our existing stylesheets to work with "select2".
$styles = $v['options'];
$o[$k]['options'] = array();
foreach ( $styles as $i => $j ) {
$o[$k]['options'][$j] = $j . ' (' . esc_attr( $theme_dir ) . ')';
}
if ( is_child_theme() ) {
$styles = woo_custom_scan_for_styles( get_stylesheet_directory() );
$o[$k]['options'] = array_merge( $o[$k]['options'], $styles );
}
}
return $o;
} // End woo_options_add()
/**
* Scan a specified directory for alt styles, and add the relative path to an array.
* @param string $dir The current child theme's directory.
* @return array
*/
function woo_custom_scan_for_styles ( $dir ) {
$styles = array();
if ( is_dir( $dir ) ) {
if ($dir_open = opendir( $dir . '/styles/' ) ) {
while ( ($file = readdir( $dir_open ) ) !== false ) {
if( stristr( $file, '.css' ) !== false ) {
$styles['../../' . basename( $dir ) . '/styles/' . $file] = $file . ' (' . esc_attr( basename( $dir ) ) . ')'; // Add the full relative path, to avoid having to override another function.
}
}
}
}
return $styles;
} // End woo_custom_scan_for_styles()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment